Indexed textboxes
-
Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!
-
Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!
Yes it is, but obviously you won't be able to use it from the designer. You can create them like
class MyForm : Form
{
private TextBox []boxes;public MyForm(int count)
{
boxes = new TextBox[count];
foreach(TextBox box in boxes)
{
// Set each box's locations.
this.Controls.Add(box);
}
}
}Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro -
Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!
Yes. If you are creating the text boxed dynamically then it is easy enough to put them in an array. If the text boxes are created in the designer then you need to populate an array after InitialiseComponent* is called - the array should be declared as a member variable in your form class. You can then refer to the array and iterate through it. * DISCLAIMER: This may not be the exact name of the method, I'm going from memory here. In any event I am referring to the method that Visual Studio modifies when you make changes in the designer.
My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
Yes it is, but obviously you won't be able to use it from the designer. You can create them like
class MyForm : Form
{
private TextBox []boxes;public MyForm(int count)
{
boxes = new TextBox[count];
foreach(TextBox box in boxes)
{
// Set each box's locations.
this.Controls.Add(box);
}
}
}Regards
Senthil
_____________________________
My Blog | My Articles | WinMacro -
Yes. If you are creating the text boxed dynamically then it is easy enough to put them in an array. If the text boxes are created in the designer then you need to populate an array after InitialiseComponent* is called - the array should be declared as a member variable in your form class. You can then refer to the array and iterate through it. * DISCLAIMER: This may not be the exact name of the method, I'm going from memory here. In any event I am referring to the method that Visual Studio modifies when you make changes in the designer.
My: Blog | Photos WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More
-
Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!
An alternative approach could be to generate the array dynamically, the framework is already storing references to all of the controls, both static and dynamic, so you can just recurse through the ControlCollections pulling out all of the ones of the required type.
private void searchControlTree(ControlCollection colControls , ArrayList allCtls, System.Type ctlType) { foreach (Control c in colControls ) { if ( c.GetType() == ctlType) allCtls.Add(c) ; if (c.Controls.Count > 0 ) searchControlTree(c.Controls, allCtls, ctlType ) ; } } public ArrayList findControlByType( System.Type ctlType) { ArrayList allCtls = new ArrayList(); searchControlTree(this.Controls, allCtls, ctlType) ; return allCtls ; } ArrayList a = findControlByType( typeof(TextBox)) ;
If you need a real array at the end the ArrayList has a ToArray() method. If you combine this approach to finding the controls together with the controlName
orTag
properties you can filter out whatever controls you want. Hope this is usefull Jackson -
Hi! Is it possible to index textboxes? What I mean is: instead of having TextBox1, TextBox2 and TextBox3 have something like an array of textboxes - TextBox(1), TextBox(2), TextBox(3) - and be able to accessed them with, for example, a FOR statement: for (int i=0; i!=4; i++) { TextBox(i).Text = "hello"; } Thank you for helping!
I think there is better way to use for or foreach for with textbox. Example make the textboxes inside a groupbox and then:
//the groupbox called gb foreach(Control c in gb) { //if teh current controll is a textbox if(c.GetType().ToString()=="System.Windows.Forms.TextBox") { c.Text="Hello"; } }
I often use this trick to clear all trextboxes in my form :->