Loop through Textboxes

2019-01-01 09:19发布

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?

12条回答
十年一品温如言
2楼-- · 2019-01-01 09:43

Iterate through controls within form and check name of the control if matched then set Text property as you require.

int i = 0;
foreach (Control contrl in this.Controls) {
    if (contrl.Name == ("DateTextBox" + i.ToString())) {
        contrl.Text = "requiredtexttobeset";
    }
    i = i + 1;
}
查看更多
只靠听说
3楼-- · 2019-01-01 09:44

You can create a Dictionary of TextBox, int like the following

Dictionary<TextBox, int> textBoxes = new Dictionary<TextBox, int>();

foreach (TextBox control in Controls.OfType<TextBox>())
    textBoxes[control] = Convert.ToInt32(control.Name.Substring(11));

Now.. to loop through them..

foreach (var item in textBoxes.Select(p => new { textBox = p.Key, no = p.Value}))
     item.textBox.Text = item.no.ToString(); // whatever you want...

Good luck!

查看更多
长期被迫恋爱
4楼-- · 2019-01-01 09:46

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.

public List GetTextBoxes(){   
    var textBoxes = new List();   
        foreach (Control c in Controls){   
            if(c is TextBox){   
                textBoxes.add(c);   
        }   
    }   
return textBoxes;   
}
查看更多
怪性笑人.
5楼-- · 2019-01-01 09:47
        //THE EASY WAY! Always post easy solutions. It's the best way.
        //This code is used to loop through all textboxes on a form for data validation. 
        //If an empty textbox is found, Set the error provider for the appropriate textbox.
        foreach (var control in Controls)
        {
            if (control is TextBox)
            {
                //Box the control into a textbox. Not really needed, but do it anyway
                var textbox = (TextBox)control;

                if (String.IsNullOrWhiteSpace(textbox.Text))
                {
                    //Set the errorProvider for data validation
                    errorProvider1.SetError(textbox, "Data Required!");
                    textbox.Text = String.Empty; //Clear out the whitespace if necessary
                    //blnError = true;
                }
            }
        }
查看更多
查无此人
6楼-- · 2019-01-01 09:47

You can simply do this mate...

foreach (TextBox txt in this.Panel.Controls.OfType<TextBox>())
            {
                txt.Text="some value you assign";
            }

If your text boxes are on the form directly and not on a Panel then you can replace this.Panel.Controls with this.Controls. That should be short and clear enough for you.

查看更多
弹指情弦暗扣
7楼-- · 2019-01-01 09:50

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#

查看更多
登录 后发表回答