Click here to Skip to main content
15,911,531 members
Articles / Programming Languages / Visual Basic
Tip/Trick

How to Create Control Array with Event

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
23 Mar 2011CPOL 14.3K   2   4
Add to Layout, and Add Event
I want to create 100 buttons in a Form. This is what I just tested.

I do have the images and I want to show you, but I could not attach those very nice pictures. When you finish the following steps, you will be able to see it. Enjoy.

Step 1: Create a Form called Form1.
Step 2: Add a TableLayoutPanel called TableLayoutPanel1.
Step 3: Make it 10 X 10 with 10% each.
Step 4: Set its anchor to T, B, L, and R. (It could be in any size in design time)
Step 5: Copy the code to Form1.vb.
Step 6: Run it, and you get it.

VB
Public Class Form1
    Private NRow As Integer = 10
    Private NCol As Integer = 10
    Private BtnArray(NRow * NCol - 1) As Button
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TableLayoutPanel1.Size = Me.ClientSize
        For i As Integer = 0 To BtnArray.Length - 1
            BtnArray(i) = New Button()
            BtnArray(i).Anchor = AnchorStyles.Top Or AnchorStyles.Bottom Or AnchorStyles.Left Or AnchorStyles.Right
            BtnArray(i).Text = CStr(i)
            TableLayoutPanel1.Controls.Add(BtnArray(i), i Mod NCol, i \ NCol)
            AddHandler BtnArray(i).Click, AddressOf ClickHandler
        Next
    End Sub
    Public Sub ClickHandler(ByVal sender As Object, ByVal e As System.EventArgs)
        MsgBox("I am button #" & CType(sender, Button).Text)
    End Sub
End Class

License

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


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

Comments and Discussions

 
GeneralI removed step 7.......... Pin
DaveAuld23-Mar-11 6:16
professionalDaveAuld23-Mar-11 6:16 
GeneralRe: I removed step 7.......... Pin
PdotWang23-Mar-11 16:50
PdotWang23-Mar-11 16:50 
Hi,

I guess you are one of the team leaders. I appreciate your work but it took a little bit too long. I told my colleague that I posted it, but he can not see it. I have to email my draft. I spent 2 hours to make the post, but you took 24 hours to delete one line from it. Actually, you really do not have to delete it but change it to something like “If it works for you, congratulations. If you have questions, welcome to leave comments”.

As to my ability to make it confidently or not, I wish you test it and see how it works. If not, then you can vote down it. No problem. I am confident.

In the future, I will follow your 3 criticizes, I understand. But I noticed that in each of them, there is an “N” word. As a leader, one would never say “N” to discourage the members.

P. Wang
GeneralRe: I removed step 7.......... Pin
DaveAuld24-Mar-11 4:14
professionalDaveAuld24-Mar-11 4:14 
GeneralRe: I removed step 7.......... Pin
PdotWang24-Mar-11 16:19
PdotWang24-Mar-11 16:19 

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.