Click here to Skip to main content
6,822,613 members and growing! (15,659 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate License: The Code Project Open License (CPOL)

How to create Control Array in C#

By Mostafa Kaisoun

Create Button array, Label array and TextBox array in c#
C#1.0, Windows, .NET1.1, Visual-Studio, Dev
Posted:2 Nov 2007
Views:32,430
Bookmarked:25 times
Unedited contribution
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
22 votes for this article.
Popularity: 4.08 Rating: 3.04 out of 5
4 votes, 19.0%
1
2 votes, 9.5%
2
1 vote, 4.8%
3
5 votes, 23.8%
4
9 votes, 42.9%
5
Screenshot - Image1.jpg

Introduction

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.

Background

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.

Using the code

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);

About article

I see that Control array creation not difficult , but I hope the new version of Visual Studio .NET will support control array as VB6.

Finally

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Mostafa Kaisoun


Member

Location: Egypt Egypt

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 12 of 12 (Total in Forum: 12) (Refresh)FirstPrevNext
GeneralThank you PinmemberWisam E. Mohammed10:42 22 Nov '09  
GeneralRe: Thank you PinmemberMostafa Kaisoun23:32 22 Nov '09  
Generalgreat article PinmemberSandeep Kalra4:11 16 Mar '09  
GeneralRe: great article PinmemberMostafa Kaisoun13:46 16 Mar '09  
GeneralMy vote of 1 PinmemberLavrekho1:25 2 Jan '09  
GeneralAnswer of: get the index of textbox. PinmemberMostafa Kaisoun15:22 10 Aug '08  
Generalget the index of Textbox PinmemberMember 419646018:22 5 Aug '08  
GeneralMore info PinmemberY_R11:05 25 Nov '07  
GeneralWorks for me PinmemberMrGriffin10:29 14 Nov '07  
GeneralRe: Works for me PinmemberMostafa Kaisoun11:33 14 Nov '07  
GeneralI am sorry! PinmemberMostafa Kaisoun5:59 3 Nov '07  
GeneralSuggestion PinmemberRi Qen-Sin5:36 3 Nov '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.

PermaLink | Privacy | Terms of Use
Last Updated: 2 Nov 2007
Editor:
Copyright 2007 by Mostafa Kaisoun
Everything else Copyright © CodeProject, 1999-2010
Web19 | Advertise on the Code Project