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");
            }

        }
6
  • You can't do that, what environment are you working in? ASP.NET / Windows Forms etc. Commented Nov 5, 2014 at 7:33
  • txtOppsNames is a List<string>. Why do you think your txtName can be a TextBox? Commented Nov 5, 2014 at 7:35
  • You could consider creating the textboxes dynamically (not via designer), and add them to a List<TextBox> instead. Commented Nov 5, 2014 at 7: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. Commented Nov 5, 2014 at 7: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. Commented Nov 5, 2014 at 7:43

3 Answers 3

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)
       }
Sign up to request clarification or add additional context in comments.

Comments

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
      }
   } 
 }

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.