Click here to Skip to main content
Licence CPOL
First Posted 2 Nov 2007
Views 51,477
Downloads 753
Bookmarked 32 times

How to Create a Control Array in C#

By | 2 Nov 2007 | Article
Create a Button array, Label array, and a TextBox array in C#.

Screenshot - Image1.jpg

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 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(); 
            } 
            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:

  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.

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.

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



Egypt Egypt

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank you Pinmemberdblick7:56 31 Jan '12  
GeneralRe: Thank you PinmemberMostafa Kaisoun11:35 31 Jan '12  
GeneralThanks a million PinmemberLesego Mashikinya2:37 15 Oct '10  
GeneralRe: Thanks a million PinmemberMostafa Kaisoun6:43 15 Oct '10  
Generalthank youuuuuuuuuuuuuuuu Pinmemberkhiat8:52 4 May '10  
GeneralRe: thank youuuuuuuuuuuuuuuu PinmemberMostafa Kaisoun1:59 5 May '10  
GeneralThank you PinmemberWisam E. Mohammed9:42 22 Nov '09  
GeneralRe: Thank you PinmemberMostafa Kaisoun22:32 22 Nov '09  
Generalgreat article PinmemberSandeep Kalra3:11 16 Mar '09  
GeneralRe: great article PinmemberMostafa Kaisoun12:46 16 Mar '09  
GeneralMy vote of 1 PinmemberLavrekho0:25 2 Jan '09  
GeneralAnswer of: get the index of textbox. PinmemberMostafa Kaisoun14:22 10 Aug '08  
Generalget the index of Textbox PinmemberMember 419646017:22 5 Aug '08  
GeneralMore info PinmemberY_R10:05 25 Nov '07  
GeneralWorks for me PinmemberMrGriffin9:29 14 Nov '07  
GeneralRe: Works for me PinmemberMostafa Kaisoun10:33 14 Nov '07  
GeneralI am sorry! PinmemberMostafa Kaisoun4:59 3 Nov '07  
GeneralSuggestion PinmemberRi Qen-Sin4:36 3 Nov '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web02 | 2.5.120517.1 | Last Updated 2 Nov 2007
Article Copyright 2007 by Mostafa Kaisoun
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid