Click here to Skip to main content
13,198,563 members (55,454 online)
Click here to Skip to main content
Add your own
alternative version


37 bookmarked
Posted 2 Nov 2007

How to Create a Control Array in C#

, 2 Nov 2007
Rate this:
Please Sign up or sign in to vote.
Create a Button array, Label array, and a TextBox array in C#.

Screenshot - Image1.jpg


I wrote an article earlier to create an array of PictureBox controls in your form. You can find it here: This article is about creating an array of Button controls, Label controls, and TextBox controls in your form.


My article has a Form (frmCotrol) with the following controls:

  • Panel control (pnlButtons) to hold an array of Buttons
  • Panel control (pnlLabels) to hold an array of Labels
  • Panel control (pnlTextBox) to hold an array of TextBoxes
  • Button control (btnAddButton) to add an array of Buttons
  • Button control (btnRemoveButton) to remove a Button
  • Button control (btnAddLabel) to add an array of Labels
  • Button control (btnRemoveLabel) to remove a Label
  • Button control (btnAddText) to add an array of TextBoxes
  • Button control (btnRemoveText) to remove a TextBox
  • Button control (btnExit) to exit the application

Using the code

I wrote a function to create an Array of Buttons, Labels, and TextBoxes 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(); 
        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(); 
        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(); 

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:

  1. The function ClickButton() will get the text of a Button.
  2. I put the event in the function ShowButton():

    // the Event of click Button
    btnArray[n].Click += new System.EventHandler(ClickButton);
  3. The function ClickLabel() will copy the text from TextBox to a Label which has the same index.
  4. I put the Event in the function ShowLabel():

    // the Event of click Label
    btnLabel[n].Click += new System.EventHandler(ClickLabel);
  5. The function ClickTextBox() will edit it.
  6. 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.


The file 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.


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


About the Author

No Biography provided

You may also be interested in...


Comments and Discussions

QuestionRealy thanks Pin
Member 1238771926-Apr-17 9:09
memberMember 1238771926-Apr-17 9:09 
QuestionThank you very... Pin
jigoch25-Jul-14 8:11
memberjigoch25-Jul-14 8:11 
AnswerRe: Thank you very... Pin
Mostafa Kaisoun25-Jul-14 15:38
memberMostafa Kaisoun25-Jul-14 15:38 
QuestionThis is good info, thanks Pin
Member 1070799928-Mar-14 18:22
memberMember 1070799928-Mar-14 18:22 
AnswerRe: This is good info, thanks Pin
Mostafa Kaisoun29-Mar-14 5:46
memberMostafa Kaisoun29-Mar-14 5:46 
GeneralThank you Pin
dblick31-Jan-12 7:56
memberdblick31-Jan-12 7:56 
GeneralRe: Thank you Pin
Mostafa Kaisoun31-Jan-12 11:35
memberMostafa Kaisoun31-Jan-12 11:35 
GeneralThanks a million Pin
Lesego Mashikinya15-Oct-10 2:37
memberLesego Mashikinya15-Oct-10 2:37 
GeneralRe: Thanks a million Pin
Mostafa Kaisoun15-Oct-10 6:43
memberMostafa Kaisoun15-Oct-10 6:43 
Generalthank youuuuuuuuuuuuuuuu Pin
khiat4-May-10 8:52
memberkhiat4-May-10 8:52 
GeneralRe: thank youuuuuuuuuuuuuuuu Pin
Mostafa Kaisoun5-May-10 1:59
memberMostafa Kaisoun5-May-10 1:59 
GeneralThank you Pin
Wisam E. Mohammed22-Nov-09 9:42
memberWisam E. Mohammed22-Nov-09 9:42 
GeneralRe: Thank you Pin
Mostafa Kaisoun22-Nov-09 22:32
memberMostafa Kaisoun22-Nov-09 22:32 
Generalgreat article Pin
Sandeep Kalra16-Mar-09 3:11
memberSandeep Kalra16-Mar-09 3:11 
GeneralRe: great article Pin
Mostafa Kaisoun16-Mar-09 12:46
memberMostafa Kaisoun16-Mar-09 12:46 
GeneralAnswer of: get the index of textbox. Pin
Mostafa Kaisoun10-Aug-08 14:22
memberMostafa Kaisoun10-Aug-08 14:22 
Generalget the index of Textbox Pin
Member 41964605-Aug-08 17:22
memberMember 41964605-Aug-08 17:22 
GeneralMore info Pin
Y_R25-Nov-07 10:05
memberY_R25-Nov-07 10:05 
GeneralWorks for me Pin
MrGriffin14-Nov-07 9:29
memberMrGriffin14-Nov-07 9:29 
GeneralRe: Works for me Pin
Mostafa Kaisoun14-Nov-07 10:33
memberMostafa Kaisoun14-Nov-07 10:33 
GeneralI am sorry! Pin
Mostafa Kaisoun3-Nov-07 4:59
memberMostafa Kaisoun3-Nov-07 4:59 
GeneralSuggestion Pin
Ri Qen-Sin3-Nov-07 4:36
memberRi Qen-Sin3-Nov-07 4:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171020.1 | Last Updated 2 Nov 2007
Article Copyright 2007 by Mostafa Kaisoun
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid