Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Creating PictureBox Array Event Handler at Runtime 1

Status
Not open for further replies.

Agent00Kevin

Programmer
Dec 26, 2008
5
I'm programming a simple game that uses an 8 by 8 game board (just like a chess board). Each square is part of a PictureBox array that is created at runtime. I used the following code:

Dim picSquareImage(8, 8) As PictureBox

I could never figure out how to create a PictureBox array at Designtime. If this is possible, please let me know as that would be the easiest way to solve my problems.

Anyhow, I successfully created, loaded, and placed the images on my form. However, the game requires that I program what happens when you click on or hover the mouse over the individual PictureBoxes. I cannot figure out though how to program the events. I tried the following code:

Private Sub picSquareImage(1, 1)_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picSquareImage(1, 1).Click

DetermineIfMoveIsAllowed(picSquareImage(1, 1), 1, 1)
If MoveIsAllowed Then
MakeMove(picSquareImage(1, 1), 1, 1)
End If

End Sub

This code, of course, is designed to handle the event of the user clicking on the square (PictureBox) in row 1, column 1.

The problem is that VB doesn't recognize picSquareImage(1, 1) or any of the 64 PictureBoxes in the array because they are not created until Runtime.

My question is how do I create the code at Runtime to handle various events or how do I create a multidimensional PictureBox array at design time? Any help would be greatly appreciated.
 
1. Create a generic event handler:
Code:
    Private Sub PictureBox_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
         'Do Something
         'Get the PictureBox for this event by casting sender:
         'Dim pb As PictureBox = CType(sender, PictureBox)
    End Sub


2. Assign each element of your array to the handler
Code:
For i As Integer = 0 To 7
   For j As Integer = 0 To 7
      Dim pb As PictureBox = YourArray(i,j)
      AddHandler pb, AddressOf Me.PictureBox_Click
   Next
Next
 
Thanks, I got it to work. However, I got an error with the AddHandler statement. I got it to work by using:

AddHandler pb.Click, AddressOf Me.PictureBox_Click

(therefore adding the ".Click")

Just wanted to confirm that this sounds correct to you.
 
Thanks again! Now that I have everything in place, I need to figure out how to determine which PictureBox in the array caused the event. I looked at the example code from the help menu, (which is for a button) but it doesn't work. This is the code in the example:

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click

Dim b As Button
b = CType(sender, Button)

Select Case b.ID
Case "Button1"
Label1.Text = "You clicked the first button"
Case "Button2"
Label1.Text = "You clicked the second button"
Case "Button3"
Label1.Text = "You clicked the third button"
End Select
End Sub

I have a few questions:

1. Do I need the "Handles..." part on the "Private Sub..." line? Obviously I can't list all 64 PictureBoxes in the array that this Sub handles.
2. When I used the "Select Case pb.ID" in my code it says that ID is not a member of System.Windows.Forms.PictureBox. What should I use to determine which PictureBox in the array caused the event?
3. Once it is determined, I need to extract the array coordinates from the PictureBox that caused the event. I thought I might be able to reference the index, but I can't find anything. My array was defined as follows:

Dim picGameSquare(8, 8) As PictureBox

So if picGameSquare(2, 3) was the PictureBox that was clicked, I would need to set 2 variables = to "2" and "3" respectively to accomplish the necessary code. How do I reference this?

Any help is greatly appreciated. Thanks again!
 
I would use the Name property of the PictureBox's when you create them. For example, when you're loading your array:

PB(0).Name = "PictureBox0"
PB(1).Name = "PictureBox1"
'etc.

Then you can use select case case against the name in your event handler

Select Case CType(sender, PictureBox).Name
Case "PictureBox0"
Case "PictureBox1"
'etc.

...However, I would attempt to make the whole thing more dynamic. Why use an array if you're going to have to write a case statement for each element? If you are able to do so, I would suggest to try to accomplish this without individual logic for each element of the array in your event handler. You can get the coordinates just fine without the case statement. Which variables are you setting with them?
 

You mentioned that I can get the coordinates just fine without a case statement. How do I do that? If someone clicks the square in the 3rd row, 4th column for example, that would be picGameSquare(3, 4). I need to be able to grab the 3 and the 4 to use it in my code. I will be comparing the game piece in the square to the ones above, below and diagonally from the clicked square. The index property would seem logical, but I'm fairly certain there isn't one for a multidimensional array. If I had to use the name property, I suppose I could parse the name and extract the coordinates from it, but that seems unnecessarily clunky. Any suggestions?

 
I thought you meant the Location when you mentioned coordinates. That being said, you can use the Tag property of the control to set extra information about the control.

Here's an example:

Code:
    Structure Coordinate
        Private m_X As Integer
        Private m_Y As Integer
        Sub New(ByVal _x As Integer, ByVal _y As Integer)
            m_X = _x
            m_Y = _y
        End Sub
        Public Property X() As Integer
            Get
                Return m_X
            End Get
            Set(ByVal value As Integer)
                m_X = value
            End Set
        End Property
        Public Property Y() As Integer
            Get
                Return m_Y
            End Get
            Set(ByVal value As Integer)
                m_Y = value
            End Set
        End Property

'............
MyPictureBox.Tag = New Coordinate(4, 3)

'............
Dim c As Coordinate = CType(MyPictureBox.Tag, Coordinate)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top