Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I respond to events raised by programmaticly created UI elements?
    primarykey
    data
    text
    <p>I'm creating a board game for a piece of coursework. For the board, I'm using some nested For loops running through a 2D array to generate a "Space" object at each square.</p> <p>The Space object contains a picturebox and some data about that space.</p> <p>How can I handle events caused by clicking on the generated picturebox without having to hard-code it for each space?</p> <p>I noticed <a href="https://stackoverflow.com/questions/13023343/how-to-get-windows-for-to-raise-mouseclick-event-for-programatically-created-pic">this</a> question seems to address this, but it's in C# and I couldn't translate it to VB.Net.</p> <p>Thanks in advance!</p> <p>Edit: This is how the board is generated</p> <pre><code> Dim board(23, 24) As Space Private Sub GenerateBoard() Dim spaceSize As New Size(30, 30) Dim spaceLocation As New Point Dim validity As Boolean For Y = 0 To 24 For X = 0 To 23 spaceLocation.X = 6 + (31 * X) spaceLocation.Y = 6 + (31 * Y) If validSpaces(Y).Contains(X + 1) Then validity = True Else validity = False End If board(X, Y) = New Space(validity, spaceSize, spaceLocation) Me.Controls.Add(board(X, Y).imageBox) board(X, Y).imageBox.BackColor = Color.Transparent board(X, Y).imageBox.BringToFront() Next Next End Sub </code></pre> <p>Space Class:</p> <pre><code>Public Class Space Dim _active As Boolean Dim _imageBox As PictureBox Public Sub New(ByVal activeInput As Boolean, ByVal size As Size, ByVal location As Point) _active = activeInput _imageBox = New PictureBox With _imageBox .Size = size .Location = location .Visible = False End With End Sub Property active As Boolean Get Return _active End Get Set(value As Boolean) _active = value End Set End Property Property imageBox As PictureBox Get Return _imageBox End Get Set(value As PictureBox) _imageBox = value End Set End Property Public Sub highlight() With _imageBox .Image = My.Resources.Highlighted_slab .Visible = True End With End Sub End Class </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload