Click here to Skip to main content
15,886,056 members
Articles / Programming Languages / Visual Basic

Easy Code to Create Button Array

Rate me:
Please Sign up or sign in to vote.
4.22/5 (19 votes)
3 Dec 2010CPOL1 min read 132.5K   4.1K   22   17
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

C#
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

VB.NET
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)


Written By
Egypt Egypt
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Question5 because it worked Pin
ancich6-Mar-20 17:37
ancich6-Mar-20 17:37 
QuestionNeeded one modification to get this to work Pin
Member 79306404-Sep-18 11:39
Member 79306404-Sep-18 11:39 
GeneralMy vote of 4 Pin
Rad.Application15-Sep-12 19:16
Rad.Application15-Sep-12 19:16 
GeneralRe: My vote of 4 Pin
Mostafa Kaisoun18-Sep-12 14:51
Mostafa Kaisoun18-Sep-12 14:51 
GeneralPage Not Found Pin
Global Analyser3-Dec-10 12:23
Global Analyser3-Dec-10 12:23 
GeneralRe: Page Not Found Pin
Mostafa Kaisoun3-Dec-10 13:19
Mostafa Kaisoun3-Dec-10 13:19 
GeneralMy vote of 3 Pin
Sivaraman Dhamodharan3-Dec-10 0:46
Sivaraman Dhamodharan3-Dec-10 0:46 
GeneralRe: My vote of 3 Pin
Mostafa Kaisoun3-Dec-10 12:57
Mostafa Kaisoun3-Dec-10 12:57 
GeneralMy vote of 2 Pin
Khaniya30-Nov-10 2:44
professionalKhaniya30-Nov-10 2:44 
General[My vote of 1] tag this article as basic Pin
Fabio Barbieri29-Nov-10 21:34
Fabio Barbieri29-Nov-10 21:34 
Hi Mostafa,
please explain that this article is for junior developer.
thanks Smile | :)
barf

GeneralMy vote of 1 Pin
Espiritu29-Nov-10 6:19
Espiritu29-Nov-10 6:19 
GeneralRe: My vote of 1 Pin
Mostafa Kaisoun29-Nov-10 12:02
Mostafa Kaisoun29-Nov-10 12:02 
GeneralMy vote of 1 Pin
omie654129-Nov-10 3:32
omie654129-Nov-10 3:32 
GeneralRe: My vote of 1 Pin
Mostafa Kaisoun29-Nov-10 11:58
Mostafa Kaisoun29-Nov-10 11:58 
GeneralRe: My vote of 1 Pin
kornakar11-Jan-11 23:46
kornakar11-Jan-11 23:46 
GeneralA suggestion for improvement Pin
Danny Sella28-Nov-10 22:23
Danny Sella28-Nov-10 22:23 
GeneralRe: A suggestion for improvement Pin
Mostafa Kaisoun29-Nov-10 11:52
Mostafa Kaisoun29-Nov-10 11:52 

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.