Click here to Skip to main content
Click here to Skip to main content

Easy Code to Create Button Array

, 3 Dec 2010
Rate this:
Please Sign up or sign in to vote.
Few lines to create twenty-six buttons for alphabetical English characters (A..Z)

img069.JPG

Introduction

I had a trial before to write phone index program using VB6. I wanted to add twenty-six buttons to my form for alphabetical English characters A..Z (one button holds one character) to search for records in the database file of my phone index.

I found it easy because VB6 allows you to create an array of buttons or an array of any control.
When I wanted to re-write the same program using C# or VB.NET, I found the addition of twenty-six buttons to the form impractical because Visual Studio .NET does not have the same possibility as VB6 to create an array of controls.

In this article you will find it very easy to create an array of buttons, you can use this idea to create a tool for searching in database file.

I will write another article later to explain how to use this idea to search within the phone index as a database.

Background

I created two projects. I wrote code of one under C# (2003) and write another under VB.NET (2003). You can read my two projects which can be found in the download links at the top of this article.

Using the Code

C# Code

private void AddButtons()
{ 
    int xPos = 0; 
    int yPos = 0; 
    // Declare and assign number of buttons = 26 
    System.Windows.Forms.Button[] btnArray = new System.Windows.Forms.Button[26]; 
    // Create (26) Buttons: 
    for (int i = 0; i < 26; i++) 
    { 
        // Initialize one variable 
        btnArray[i] = new System.Windows.Forms.Button(); 
    } 
    int n = 0; 

    while(n < 26) 
    { 
        btnArray[n].Tag = n + 1; // Tag of button 
        btnArray[n].Width = 24; // Width of button 
        btnArray[n].Height = 20; // Height of button 
        if(n == 13) // Location of second line of buttons: 
        { 
            xPos = 0; 
            yPos = 20; 
        } 
        // Location of button: 
        btnArray[n].Left = xPos; 
        btnArray[n].Top = yPos; 
        // Add buttons to a Panel: 
        pnlButtons.Controls.Add(btnArray[n]); // Let panel hold the Buttons 
        xPos = xPos + btnArray[n].Width; // Left of next button 
        // Write English Character: 
        btnArray[n].Text = ((char)(n + 65)).ToString(); 

        /* **************************************************************** 
        You can use following code instead previous line 
        char[] Alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 
        'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 
        'W', 'X', 'Y', 'Z'}; btnArray[n].Text = Alphabet[n].ToString(); 
        //**************************************************************** */ 

        // the Event of click Button 
        btnArray[n].Click += new System.EventHandler(ClickButton); 
        n++; 
    } 
    btnAddButton.Enabled = false; // not need now to this button now 
    label1.Visible = true; 
} 

// Result of (Click Button) event, get the text of button 
public void ClickButton(Object sender, System.EventArgs e) 
{ 
    Button btn = (Button) sender; 
    MessageBox.Show("You clicked character [" + btn.Text + "]"); 
}  

VB Code

Private Sub AddButtons()
    Dim xPos As Integer = 0
    Dim yPos As Integer = 0
    Dim n As Integer = 0
    ' Declare and Initialize one variable    
    Dim btnArray(26) As System.Windows.Forms.Button    
    For i As Integer = 0 To 25        
        btnArray(i) = New System.Windows.Forms.Button
    Next i

    While (n < 26)
        With (btnArray(n))
            .Tag = n + 1 ' Tag of button
            .Width = 24 ' Width of button
            .Height = 20 ' Height of button
            If (n = 13) Then ' Location of second line of buttons:
                xPos = 0
                yPos = 20
            End If            ' Location of button:            .Left = xPos
            .Top = yPos
            ' Add buttons to a Panel:            
            pnlButtons.Controls.Add(btnArray(n)) ' Let panel hold the Buttons
            xPos = xPos + .Width ' Left of next button
            ' Write English Character:            .Text = Chr(n + 65)

            ' ****************************************************************
            ' You can use following code instead previous line
            ' Dim Alphabet() As Char = {"A", "B", "C", "D", "E", "F", "G", _
            ' "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", _
            ' "U", "V", "W", "X", "Y", "Z"}
            ' .Text = Alphabet(n)
            ' ****************************************************************
            ' for Event of click Button            
            AddHandler .Click, AddressOf Me.ClickButton
            n += 1
        End With
    End While
    btnAddButton.Enabled = False ' not need now to this button now
    label1.Visible = True
End Sub
' Result of (Click Button) event, get the text of button
Public Sub ClickButton(ByVal sender As Object, ByVal e As System.EventArgs) _
	ndles MyBase.Click
    Dim btn As Button = sender
    MessageBox.Show("You clicked character [" + btn.Text + "]")
End Sub 

Final Words

I hope this article is useful. If you have any ideas please tell me. Thanks to CodeProject and thanks to 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

No Biography provided

Comments and Discussions

 
GeneralMy vote of 4 PinmemberRad.Application15-Sep-12 19:16 
GeneralRe: My vote of 4 PinmemberMostafa Kaisoun18-Sep-12 14:51 
GeneralPage Not Found PinmemberHeaven20203-Dec-10 12:23 
GeneralRe: Page Not Found PinmemberMostafa Kaisoun3-Dec-10 13:19 
GeneralMy vote of 3 Pinmembersirama20043-Dec-10 0:46 
GeneralRe: My vote of 3 PinmemberMostafa Kaisoun3-Dec-10 12:57 
GeneralMy vote of 2 PinmemberKhaniya30-Nov-10 2:44 
General[My vote of 1] tag this article as basic PinmemberFabio Barbieri29-Nov-10 21:34 
GeneralMy vote of 1 PinmemberEspiritu29-Nov-10 6:19 
GeneralRe: My vote of 1 PinmemberMostafa Kaisoun29-Nov-10 12:02 
GeneralMy vote of 1 Pinmemberomie654129-Nov-10 3:32 
GeneralRe: My vote of 1 PinmemberMostafa Kaisoun29-Nov-10 11:58 
GeneralRe: My vote of 1 Pinmemberkornakar11-Jan-11 23:46 
GeneralA suggestion for improvement PinmemberDanny Sella28-Nov-10 22:23 
GeneralRe: A suggestion for improvement PinmemberMostafa Kaisoun29-Nov-10 11:52 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 3 Dec 2010
Article Copyright 2010 by Mostafa Kaisoun
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid