![]() |
Languages »
C# »
General
Intermediate
How to create Control Array in C#By Mostafa KaisounCreate Button array, Label array and TextBox array in c# |
C# 1.0, Windows, .NET 1.1, Visual Studio, Dev
|
||||||||
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
I wrote an article before to create Array of PictureBox controls You can find it here:
PictureBox_Array.asp
Now I write another article to create Array of Button controls, Label controls and TextBox controls.
My article has one Form (frmCotrol), This form has following comtrols:
Panel control (pnlButtons) to hold array of Buttons,
Panel control (pnlLabels) to hold array of Labels,
Panel control (pnlTextBox) to hold array of TextBoxs,
Button control (btnAddButton) to add array of Buttons,
Button control (btnRemoveButton) to remove a Button,
Button control (btnAddLabel) to add array of Labels,
Button control (btnRemoveLabel) to remove a Label,
Button control (btnAddText) to add array of TextBoxs,
Button control (btnRemoveText) to remove a TextBox,
Button control (btnExit) to exit application.
I wrote one function to create Array of Buttons, Labels, TextBoxs 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 Events and the result of these events which occurs when the control is clicked:
1- The function ClickButton() will view the text of Button.
I put the Event in the function ShowButton():
//
// the Event of click Button
//
btnArray[n].Click += new System.EventHandler(ClickButton);
2- The function ClickLabel () will copy the text from TextBox to Label which has same index if any.
I put the Event in the function ShowLabel():
//
// the Event of click Label
//
btnLabel[n].Click += new System.EventHandler(ClickLabel);
3- The function ClickTextBox () will tell you to edit it.
I put the Event in the function ShowTextBox():
//
// the Event of click TextBox
//
btn TextBox [n].Click += new System.EventHandler(ClickTextBox);
I see that Control array creation not difficult , but I hope the new version of Visual Studio .NET will support control array as VB6.
The file ControlArray.zip contain Source files of this article.
I hope this article helps You to create array of any control. If You have any idea I hope to know and if You find any problem You can tell me.
Sorry if You find some errors in my English language!
Thanks for Code project and for all.
Mostafa Kaisoun
M_Kaisoun@hotmail.com
| You must Sign In to use this message board. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 2 Nov 2007 Editor: |
Copyright 2007 by Mostafa Kaisoun Everything else Copyright © CodeProject, 1999-2009 Web19 | Advertise on the Code Project |