JavaScript Editor JavaScript Editor     JavaScript Debugger

Previous Section Next Section

Main Page

Imitating Control Arrays

You've decided that your new game program really does need 144 buttons in the main form, arranged in a grid of 12 × 12. But what a pain it is to write 144 subroutines to handle the click event for each of them! Isn't there a better way?

There used to be. In VB6 and before, you could use a control array and one event-handler function. The control array index of the button that was clicked was passed to the event handler, so you could tell which button you needed to respond to. To create a control array, you just gave two controls of the same type the same name (in the Name property); when you did, Visual Basic would ask if you wanted to create a control array.

Control arrays don't exist in Visual Basic .NET—at least, they're not built in anymore. However, you can still create something very like a control array yourself, in code. The main feature of control arrays is that all the controls in it share the same event handler, and you can use the AddHandler method to assign the same event handler to multiple controls.

Here's an example, named ControlArray on the CD-ROM. In this example, I'll create three buttons in code and give them all the same click event handler, Button_Click. Because the actual button that caused the event is passed to the event handler, we can determine which button was clicked. Note that although I'm creating the buttons in code here, you do not need to create them at run time—you can create them at design time and use AddHandler at run time to connect them into a control array.

I start by declaring the new buttons we'll use, Button1, Button2, and Button3. To create the control array in the ControlArray example, do the following (note that I use the WithEvents keyword to indicate that these objects will handle events):

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim WithEvents Button1 As Button
    Dim WithEvents Button2 As Button
    Dim WithEvents Button3 As Button
        

And I add another button to Form1 at design time, which I call Button4 and give the caption "Create control array". When the user clicks this button, I create the new button objects, set their sizes and locations, and add them to the form's Controls collection to add them to the form (see "Adding and Removing Controls at Run Time" in Chapter 4 for more information):

    Private Sub Button4_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button4.Click
        Button1 = New Button()
        Button2 = New Button()
        Button3 = New Button()

        Button1.Size = New Size(80, 30)
        Button1.Location = New Point(115, 20)
        Button1.Text = "Button 1"

        Button2.Size = New Size(80, 30)
        Button2.Location = New Point(115, 60)
        Button2.Text = "Button 2"

        Button3.Size = New Size(80, 30)
        Button3.Location = New Point(115, 100)
        Button3.Text = "Button 3"

        Controls.Add(Button1)
        Controls.Add(Button2)
        Controls.Add(Button3)
        
    End Sub

To imitate a control array with the three new buttons, I use AddHandler to connect the same event handler, Button_Click, to all three button's Click events. To connect that event handler, you need to pass its memory address using the AddressOf operator to AddHandler:

    Private Sub Button4_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button4.Click
        Button1 = New Button()
        Button2 = New Button()
        Button3 = New Button()
       
        AddHandler Button1.Click, AddressOf Button_Click
        AddHandler Button2.Click, AddressOf Button_Click
        AddHandler Button3.Click, AddressOf Button_Click
    End Sub

Now in Button_Click, I can determine which button was clicked and display a corresponding message in a text box, TextBox1:

    Private Sub Button_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs)
        If sender Is Button1 Then
            TextBox1.Text = "You clicked button 1"
        End If
        If sender Is Button2 Then
            TextBox1.Text = "You clicked button 2"
        End If
        If sender Is Button3 Then
            TextBox1.Text = "You clicked button 3"
        End If
    End Sub

And that's it—now all three buttons share the same event handler, just as they would have in a control array in earlier versions of Visual Basic. Here's the whole code:

Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim WithEvents Button1 As Button
    Dim WithEvents Button2 As Button
    Dim WithEvents Button3 As Button
    Friend WithEvents Button4 As Button
    Friend WithEvents TextBox1 As TextBox

    'Windows Form Designer generated code

    Private Sub Button4_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button4.Click
        Button1 = New Button()
        Button2 = New Button()
        Button3 = New Button()

        Button1.Size = New Size(80, 30)
        Button1.Location = New Point(115, 20)
        Button1.Text = "Button 1"

        Button2.Size = New Size(80, 30)
        Button2.Location = New Point(115, 60)
        Button2.Text = "Button 2"

        Button3.Size = New Size(80, 30)
        Button3.Location = New Point(115, 100)
        Button3.Text = "Button 3"

        Controls.Add(Button1)
        Controls.Add(Button2)
        Controls.Add(Button3)

        AddHandler Button1.Click, AddressOf Button_Click
        AddHandler Button2.Click, AddressOf Button_Click
        AddHandler Button3.Click, AddressOf Button_Click
    End Sub

    Private Sub Button_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs)
        If sender Is Button1 Then
            TextBox1.Text = "You clicked button 1"
        End If
        If sender Is Button2 Then
            TextBox1.Text = "You clicked button 2"
        End If
        If sender Is Button3 Then
            TextBox1.Text = "You clicked button 3"
        End If
    End Sub

End Class

You can see the results in Figure 6.8—when you click the "Create control array" button, the three top buttons appear. Clicking one of those buttons displays a message in the text box indicating which button was clicked.


Figure 6.8: Creating a control array.

Related solution:

Found on page:

Adding and Removing Controls at Run Time

196

Previous Section Next Section




JavaScript Editor Free JavaScript Editor     JavaScript Editor