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