1

I have a List object that gets automatically filled with the names of textboxes. Now I'd like to cycle through all these text boxes. How can I let C# know that the string-names in the List are actually TextBox objects with that string-name?

        List<string> txtOppsNames = new List<string>(); 
        for (int i = 1; i < numOpps; i++) 
        {
            txtOppsNames.Add("txtOpp" + i);
        }
        foreach (var txtName in txtOppsNames) 
        {
            if (txtName.Text != "")
            {
                // do stuff
            }
        }

The current code reads txtName as a string. I would like it to read as a TextBox.

Edit - the below code contains the solution for me.

        List<string> txtOppsNames = new List<string>();
        for (int i = 1; i < numOpps; i++) 
        {
            txtOppsNames.Add("txtOpp" + i);
        }
        foreach (var txtName in txtOppsNames)
        {
            TextBox textBox = this.Controls.Find(txtName, true).FirstOrDefault() as TextBox;
            if (textBox.Text != "")
            {
                MessageBox.Show("Thanks Amir Popovich");
            }

        }
Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58
Paretozen
  • 7
  • 2
  • You can't do that, what environment are you working in? ASP.NET / Windows Forms etc. – Nunners Nov 05 '14 at 07:33
  • `txtOppsNames` is a `List`. Why do you think your `txtName` can be a `TextBox`? – Soner Gönül Nov 05 '14 at 07:35
  • You could consider creating the textboxes dynamically (not via designer), and add them to a `List` instead. – C.Evenhuis Nov 05 '14 at 07:37
  • I'm using Windows Forms application. I'm used to doing similar operations in VBA-code, I'd love to use it in this environment. – Paretozen Nov 05 '14 at 07:38
  • What are you trying to do with the text? and how are you filling the list? its possible that you could use data-binding or just use a `TextBox` list as previously suggested to achieve the same goal. – Sayse Nov 05 '14 at 07:43
  • @Paretozen - two things: 1. change `if (textBox.Text != "")` to `if(textBox != null && textBox.Text != string.Empty)` since you are using `FirstOrDefault`. 2. You don't need to edit your question and add the answer- You need to accept the correct answer. – Amir Popovich Nov 05 '14 at 08:29

3 Answers3

1

Use Control.ControlCollection.Find:

string textBoxName = "txtOpp1";
TextBox textBox = this.Controls.Find(textBoxName, true).FirstOrDefault() as TextBox;

In your case:

List<string> txtOppsNames = new List<string>(); 
for (int i = 1; i < numOpps; i++) 
{
    txtOppsNames.Add("txtOpp" + i);
}
foreach (var txtName in txtOppsNames) 
{
   var control = this.Controls.Find(txtName, true).FirstOrDefault();
   if(control != null && control is TextBox)
   {
      TextBox textBox = control as TextBox;
      if(textBox.Text != string.Empty)
      {
          //logic
      }
   } 
 }
Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • 1
    I like @Royi's answer, as it has the test to ensure it is a textbox, but I also like your `textBox.Text !=string.Empty.` – jbutler483 Nov 05 '14 at 08:57
1

Try this :

  List<string> txtOppsNames = new List<string>(); 
        for (int i = 1; i < numOpps; i++) 
        {
            txtOppsNames.Add("txtOpp" + i);
        }
        foreach (var txtName in txtOppsNames) 
        {
          var cntrl= FindControl(txtName);
           if (cntrl!=null && cntrl is TextBox)
              // do something with 
              ((TextBox)cntrl)
       }
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
0

Assuming it is Winform, There seems to be no pretty way to do it.

But you could use Loop through Textboxes.

once you have the text box collection, check the names with your string.

Community
  • 1
  • 1
Ricky
  • 1
  • 1