0

I have a few different TextBox elements named as followed "e0", "e1", "e2", "e3". I know how many there are and I just want to be able to loop through them and grab their values rather than typing each one out manually.

I'm assuming I'll be doing something like this, I just don't know how to access the element.

for(int i= 0; i < 4; ++i) {
    string name = "e" + i;
    // How do I use my newly formed textbox name to access the textbox 
    // element in winforms?
}
Angelo R.
  • 2,285
  • 1
  • 17
  • 22

3 Answers3

2

You can use parent of your controls like this(Assuming you have placed all controls in the form, so I have used this)

for(int i= 0; i < 4; ++i) {
string name = "e" + i;
TextBox txtBox=this.Controls[name] as TextBox;
Console.Writeline(txtBox.Text);
}
Sudhakar B
  • 1,465
  • 9
  • 16
2

I would advise against this approach since it's prone to errors. What if you want to rename them, what if you'll forget about this and add other controls with name e...?

Instead i would collect them in a container control like Panel. Then you can use LINQ to find the relevant TextBoxes:

var myTextBoxes = myPanel.Controls.OfType<TextBox>();

Enumerable.OfType will filter and cast the controls accordingly. If you want to filter them more, you could use Enumerable.Where, for example:

var myTextBoxes = myPanel.Controls
                         .OfType<TextBox>()
                         .Where(txt => txt.Name.ToLower().StartsWith("e"));

Now you can iterate those TextBoxes, for example:

foreach(TextBox txt in myTextBoxes)
{
    String text = txt.Text;
    // do something amazing
}

Edit:

The TextBoxes are on multiple TabPages. Also, the names are a little more logical ...

This approach works also when the controls are on multiple tabpages, for example:

var myTextBoxes = from tp in tabControl1.TabPages.Cast<TabPage>()
                  from panel in tp.Controls.OfType<Panel>()
                  where panel.Name.StartsWith("TextBoxGroup")
                  from txt in panel.Controls.OfType<TextBox>()
                  where txt.Name.StartsWith("e")
                  select txt;

(note that i've added another condition that the panels names' must start with TextBoxGroup, just to show that you can also combine the conditions)

Of course the way to detect the relevant controls can be changed as desired(f.e. with RegularExpression).

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • While this solution is very nice I don't think it would work for this application :( The TextBoxes are on multiple TabPages. Also, the names are a little more logical than e[x] but I figured it wouldn't really matter for an example. Thanks for this :D If I can figure out how to re-org things without upsetting people I might do this. – Angelo R. Jun 29 '12 at 11:21
  • @AngeloR.: This approach works also when the controls are on multiple tabpages. See my edit. – Tim Schmelter Jun 29 '12 at 11:40
  • Ahh, thanks for the update Tim. I'm not too familiar with LINQ so I never even realized that it could be used for something like this. I've changed this to be the actual answer because it seems to be a little more robust, and cause it taught me a bit about LINQ :) – Angelo R. Jun 29 '12 at 16:29
0

Try this :

this.Controls.Find()

for(int i= 0; i < 4; ++i) {
    string name = "e" + i;
    TextBox txtBox = this.Controls.Find(name) as TextBox;
}

Or this :

this.Controls["name"]

for(int i= 0; i < 4; ++i) {
    string name = "e" + i;
    TextBox txtBox = this.Controls[name] as TextBox;
}
Aelios
  • 11,849
  • 2
  • 36
  • 54