How to Create a Control Array in C#






3.14/5 (25 votes)
Create a Button array, Label array, and a TextBox array in C#.
Introduction
I wrote an article earlier to create an array of PictureBox
controls in your form. You can find it here: http://www.codeproject.com/KB/cs/PictureBox_Array.aspx. This article is about creating an array of Button
controls, Label
controls, and TextBox
controls in your form.
Background
My article has a Form
(frmCotrol
) with the following controls:
Panel
control (pnlButtons
) to hold an array ofButton
sPanel
control (pnlLabels
) to hold an array ofLabel
sPanel
control (pnlTextBox
) to hold an array ofTextBox
esButton
control (btnAddButton
) to add an array ofButton
sButton
control (btnRemoveButton
) to remove aButton
Button
control (btnAddLabel
) to add an array ofLabel
sButton
control (btnRemoveLabel
) to remove aLabel
Button
control (btnAddText
) to add an array ofTextBox
esButton
control (btnRemoveText
) to remove aTextBox
Button
control (btnExit
) to exit the application
Using the code
I wrote a function to create an Array
of Button
s, Label
s, and TextBox
es at once:
//
// 'anyControl' is type of control, 'cNumber' is number of control
//
private void AddControls( string anyControl, int cNumber)
{
switch (anyControl)
{
case "btn": // anyControl = btn to Add Button
{
// assign number of controls
btnArray = new System.Windows.Forms.Button[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
btnArray[i] = new System.Windows.Forms.Button();
}
break;
}
case "lbl": // anyControl = lbl to Add Label
{
// assign number of controls
lblArray = new System.Windows.Forms.Label[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
lblArray[i] = new System.Windows.Forms.Label();
}
break;
}
case "txt": // anyControl = txt to Add TextBox
{
// assign number of controls
txtArray = new System.Windows.Forms.TextBox[cNumber + 1];
for (int i = 0; i < cNumber + 1; i++)
{
// Initialize one variable
txtArray[i] = new System.Windows.Forms.TextBox();
}
break;
}
}
}
You can go to the functions in the source file to see how I create the Events and the result of these events which occurs when the control is clicked:
- The function
ClickButton()
will get the text of aButton
. - The function
ClickLabel()
will copy the text fromTextBox
to aLabel
which has the same index. - The function
ClickTextBox()
will edit it.
I put the event in the function ShowButton()
:
//
// the Event of click Button
//
btnArray[n].Click += new System.EventHandler(ClickButton);
I put the Event in the function ShowLabel()
:
//
// the Event of click Label
//
btnLabel[n].Click += new System.EventHandler(ClickLabel);
I put the Event in the function ShowTextBox()
:
//
// the Event of click TextBox
//
btn TextBox [n].Click += new System.EventHandler(ClickTextBox);
About the code
Control array creation is not difficult, but I hope the new version of Visual Studio .NET will support control arrays as in VB6.
Finally
The file ControlArray.zip contains the source files of this article. I hope this article helps you to create arrays of any control. If you have any ideas, I would like to know about them, and all feedback is appreciated.
Sorry if you find errors in my English!
Thanks to CodeProject and all of you.