Winforms 애플리케이션에서 텍스트 상자 반복
Winforms 애플리케이션에서는 텍스트 상자 모음을 반복해야 하는 상황이 발생할 수 있습니다. 화면에. 각 텍스트 상자에는 다음과 같이 순차적으로 번호가 매겨질 수 있습니다.
DateTextBox0 DateTextBox1 ... DateTextBox37
이러한 텍스트 상자에 값을 할당하려면 다음 접근 방식을 고려할 수 있습니다.
int month = MonthYearPicker.Value.Month; int year = MonthYearPicker.Value.Year; int numberOfDays = DateTime.DaysInMonth(year, month); m_MonthStartDate = new DateTime(year, month, 1); m_MonthEndDate = new DateTime(year, month, numberOfDays); DayOfWeek monthStartDayOfWeek = m_MonthStartDate.DayOfWeek; int daysOffset = Math.Abs(DayOfWeek.Sunday - monthStartDayOfWeek); for (int i = 0; i <= (numberOfDays - 1); i++) { // Here is where you want to loop through the textboxes and assign values based on the 'i' value // DateTextBox(daysOffset + i) = m_MonthStartDate.AddDays(i).Day.ToString(); }
그러나 애플리케이션에서는 이러한 텍스트 상자는 별도의 패널에 위치하므로 복잡성이 추가됩니다. 이러한 컨트롤을 효과적으로 반복하려면 확장 메서드를 활용할 수 있습니다.
public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control { var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>(); return children.SelectMany(c => GetChildControls<TControl>(c)).Concat(children); }
이 확장 메서드를 사용하면 지정된 유형의 모든 컨트롤과 하위 컨트롤을 재귀적으로 검색할 수 있습니다. 사용 예:
var allTextBoxes = this.GetChildControls<TextBox>(); foreach (TextBox tb in allTextBoxes) { tb.Text = ...; }
이 확장 방법을 사용하면 별도의 패널에 있는 모든 텍스트 상자를 효율적으로 반복하고 원하는 논리에 따라 값을 할당할 수 있습니다.
위 내용은 WinForms 응용 프로그램의 여러 패널에 있는 텍스트 상자를 통해 효율적으로 반복하려면 어떻게 해야 합니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!