Is this an acceptable way to make a Control Array? [modified]
-
Control[] controlArrays = new Control[3];
controlArray[0] = textbox1; //name of the textbox control on the form
controlArray[1] = textbox2;
controlArray[2] = textbox3;for(int i = 0; i < controlArrays.Length; i++)
{
if(controlArray[i] is TextBox)
(TextBox)controlArray[i].Text = "My Array #" + i.ToString();
}Thanks.
modified on Friday, December 4, 2009 12:13 AM
-
Control[] controlArrays = new Control[3];
controlArray[0] = textbox1; //name of the textbox control on the form
controlArray[1] = textbox2;
controlArray[2] = textbox3;for(int i = 0; i < controlArrays.Length; i++)
{
if(controlArray[i] is TextBox)
(TextBox)controlArray[i].Text = "My Array #" + i.ToString();
}Thanks.
modified on Friday, December 4, 2009 12:13 AM
You could create an array of type System.Windows.Forms.Control instead of object. After all, if you are only going to keep textboxes inside your array it does not make sense to use an object type. You could even use System.Windows.Forms.TextBoxBase if you really just need to store text boxes. Since you know your array elements anyway, you could also do something like - Control[] controlArrays = new Control[]{textbox1,textbox2,textbox3}; Looks neater, thats all ;) . I'm no expert, but I dont see any problems in storing controls inside an array.
-
You could create an array of type System.Windows.Forms.Control instead of object. After all, if you are only going to keep textboxes inside your array it does not make sense to use an object type. You could even use System.Windows.Forms.TextBoxBase if you really just need to store text boxes. Since you know your array elements anyway, you could also do something like - Control[] controlArrays = new Control[]{textbox1,textbox2,textbox3}; Looks neater, thats all ;) . I'm no expert, but I dont see any problems in storing controls inside an array.
I got an error there, i really meant Control, not object. Thanks. I asked because I saw an article regarding making Control Arrays in C# but its longer and a lot more complicated.
-
Control[] controlArrays = new Control[3];
controlArray[0] = textbox1; //name of the textbox control on the form
controlArray[1] = textbox2;
controlArray[2] = textbox3;for(int i = 0; i < controlArrays.Length; i++)
{
if(controlArray[i] is TextBox)
(TextBox)controlArray[i].Text = "My Array #" + i.ToString();
}Thanks.
modified on Friday, December 4, 2009 12:13 AM
-
Control[] controlArrays = new Control[3];
controlArray[0] = textbox1; //name of the textbox control on the form
controlArray[1] = textbox2;
controlArray[2] = textbox3;for(int i = 0; i < controlArrays.Length; i++)
{
if(controlArray[i] is TextBox)
(TextBox)controlArray[i].Text = "My Array #" + i.ToString();
}Thanks.
modified on Friday, December 4, 2009 12:13 AM
Consider using a List instead of an array, unless you have specific needs that dictate using an Array. The List class will automatically re-size as you add objects, yet you can still iterate and reference by index. For example:
List controls = new List();
controls.Add(textbox1);
controls.Add(textbox2);
controls.Add(textbox3);// ect...
controls.Add(textbox99999999999);
for(int i = 0; i < controlArrays.Length; i++)
{
if(controls[i] is TextBox)
(TextBox)controls[i].Text = "Element #" + i.ToString();
} -
Control[] controlArrays = new Control[3];
controlArray[0] = textbox1; //name of the textbox control on the form
controlArray[1] = textbox2;
controlArray[2] = textbox3;for(int i = 0; i < controlArrays.Length; i++)
{
if(controlArray[i] is TextBox)
(TextBox)controlArray[i].Text = "My Array #" + i.ToString();
}Thanks.
modified on Friday, December 4, 2009 12:13 AM
Use Arraylist and then filter the Textbox controls from the form controls to get collection of controls.
visit the link for more info : http://www.dreamincode.net/forums/showtopic45666.htm[^]
modified on Friday, December 4, 2009 5:39 AM
-
Control[] controlArrays = new Control[3];
controlArray[0] = textbox1; //name of the textbox control on the form
controlArray[1] = textbox2;
controlArray[2] = textbox3;for(int i = 0; i < controlArrays.Length; i++)
{
if(controlArray[i] is TextBox)
(TextBox)controlArray[i].Text = "My Array #" + i.ToString();
}Thanks.
modified on Friday, December 4, 2009 12:13 AM
Use a Generic List. It is much faster, resizes automatically and also avoids runtime type casts.
List<TextBox> textBoxes = new List<TextBox>():
textBoxes.Add(textBox1);
textBoxes.Add(textBox2);
textBoxes.Add(textBox2);int i =0;
foreach(TextBox textBox in textBoxes) {
textBox.Text = string.Format("My Array {0}", ++i);
} -
Control[] controlArrays = new Control[3];
controlArray[0] = textbox1; //name of the textbox control on the form
controlArray[1] = textbox2;
controlArray[2] = textbox3;for(int i = 0; i < controlArrays.Length; i++)
{
if(controlArray[i] is TextBox)
(TextBox)controlArray[i].Text = "My Array #" + i.ToString();
}Thanks.
modified on Friday, December 4, 2009 12:13 AM
-
Control[] controlArrays = new Control[3];
controlArray[0] = textbox1; //name of the textbox control on the form
controlArray[1] = textbox2;
controlArray[2] = textbox3;for(int i = 0; i < controlArrays.Length; i++)
{
if(controlArray[i] is TextBox)
(TextBox)controlArray[i].Text = "My Array #" + i.ToString();
}Thanks.
modified on Friday, December 4, 2009 12:13 AM
That looks OK, but why would you want to?