I have a winforms app that has 37 textboxes on the screen. Each one is sequentially numbered:
DateTextBox0
DateTextBox1 ...
DateTextBox37
I am trying to iterate through the text boxes and assign a value to each one:
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 I want to loop through the textboxes and assign values based on the 'i' value
DateTextBox(daysOffset + i) = m_MonthStartDate.AddDays(i).Day.ToString();
}
Let me clarify that these textboxes appear on separate panels (37 of them). So in order for me to loop through using a foreach, I have to loop through the primary controls (the panels), then loop through the controls on the panels. It starts getting complicated.
Any suggestions on how I can assign this value to the textbox?
Iterate through controls within form and check name of the control if matched then set Text property as you require.
You can create a
Dictionary
ofTextBox
,int
like the followingNow.. to loop through them..
Good luck!
You Could loop all the controls in the form asking one by one if it is a "Textbox" y ther return the complete List of them.
You can simply do this mate...
If your text boxes are on the form directly and not on a Panel then you can replace
this.Panel.Controls
withthis.Controls
. That should be short and clear enough for you.Since you already know the name of control, therefore you can search the control by its name.
See Get a Windows Forms control by name in C#