hello sir.
I want to create a form and add controls to it, after that i want to make an array for controls of same type. just like separate array for buttons, another for text boxes, etc.
and the array should be dynamic just as i increase the number of control it should also change its size. thanks in advanced
Loading
VulpesPosted Sep 18, 2011, 9:01 AM
Just use the Add() method to add dynamically created controls to the list.
theLizardPosted Sep 18, 2011, 8:05 PM
Control[] ctrls = new Control[0];
for (int i = 0; i < 5; i++)
{
Array.Resize(ref ctrls, ctrls.Length + 1);
ctrls[ctrls.Length - 1] = new TextBox();
Array.Resize(ref ctrls, ctrls.Length + 1);
ctrls[ctrls.Length - 1] = new ComboBox();
}
use.
string name = ((TextBox)ctrls[index]).Name;
}
this way you only have one array to deal with and assign any control to an array element
string s = ((TextBox)ctrls[0]).Name;
TextBox t;
if (ctrls[2].GetType().ToString() == "System.Windows.Forms.TextBox")
{
t = (TextBox)ctrls[2];
t.Text = "Hello";
}
or
ComboBox c;
if (CombotBox)ctrls[1].GetType().ToString() == "System.Windows.Forms.ComboBox")
{
c = (ComboBox)ctrls[1];
c.Text = "Hello";
}