Question Passing a panel within an array using a handler


New member
Feb 14, 2018
Programming Experience
Currently i am trying to write a chess program within vb. The initial problem i am having is using a handler to select a square and then another square, this is mimicking the process of selecting a piece to move and then a place to move it to. in the code below i have added a handler so that each panel can be clicked along with a second panel but the panel its self is selected rather than the board array that i want to be selected. i have tried using bu this does not seem to work as board is a 2d array. My question is how do i pass a board value into a subroutine using addhandler rather than just the panel.

Public Class Form1
    Dim board(7, 7) As Panel
    Dim pointer As Integer = 0
    Dim first As Panel
    Dim second As Panel

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    End Sub

    Sub loadboard()
        For x = 0 To 7
            For y = 0 To 7
                Dim pnl As New Panel
                With pnl
                    If x Mod 2 = y Mod 2 Then
                        .BackColor = Color.White
                    ElseIf x Mod 2 <> y Mod 2 Then
                        .BackColor = Color.Brown
                    End If
                    .BorderStyle = BorderStyle.FixedSingle
                    .Location = New Point(55 * x, 55 * y)
                    .Size = New Size(55, 55)
                End With
                board(x, y) = pnl
                AddHandler pnl.Click, AddressOf pnl_click
    End Sub

    Sub pnl_click(sender As Object, e As EventArgs)

        Dim clickedpnl As Panel = DirectCast(sender, Panel)
        clickedpnl.BackColor = Color.Yellow

        If pointer = 0 Then
            first = clickedpnl
            pointer = 1
            second = clickedpnl
            pointer = 0
        End If

    End Sub

End Class


VB.NET Forum Moderator
Staff member
Aug 17, 2004
Sydney, Australia
Programming Experience
Your question doesn't really make sense to me. 'board' is an array. What would it mean to "select" an array? The 'sender' parameter in an event handler is ALWAYS a reference to the object that raised the event. If you're handling the Click event of a Panel then the 'sender' IS the Panel that was clicked. If you want to access the array that your 'board' field refers to then you simply use the 'board' field.

I suspect that your post has failed to convey something important. Please try again to provide a FULL and CLEAR explanation of the problem. Try to avoid vague terms like "select" if you can. If by "select" you mean "change the BackColor of" then say that. If you mean something else, say that. Try to be as specific as you can because terms like "select" can be interpreted in various ways in situations where they have no specific definition.

By the way, I would do away with that 'pointer' field. I would suggest that you simply test whether 'first" is Nothing or not. If it is then set that and if it's not then set 'second'. That would mean setting both to Nothing explicitly after performing the move.

I would also suggest changing those names to something more descriptive than 'first' and 'second'. Variables should be nouns, possibly qualified with adjectives, not adjectives alone.
Top Bottom