Reputation: 193
I am learning VB.Net at college and I am trying to find a way to check the button clicked by the user in vb.net form. The problem I am currently having is that I have an array of 10x10 buttons, created dynamically in the code.
For x = 1 To 10
For y = 1 To 10
Me.Width = 720
Me.Height = 720
boxarray(counter) = New Button
boxarray(counter).Name = "Box" & x
boxarray(counter).Location = New Point(x * 48, y * 48)
boxarray(counter).Width = Me.Width / 15
boxarray(counter).Height = Me.Height / 15
boxarray(counter).Visible = True
boxarray(counter).Cursor = Cursors.Hand
boxarray(counter).PerformClick()
Me.Controls.Add(boxarray(counter))
counter = counter + 1
Next
Next
However I need to check the button that the user has clicked, without needing to create subroutines for each individual button, that could be pressed.
Upvotes: 0
Views: 264
Reputation: 20330
First add a Handler
e.g.
Private Sub ArrayButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
' Handle your Button clicks here
End Sub
Then change boxarray(counter).PerformClick()
(which simuates clicking the button)
to
AddHandler boxarray(counter), AddressOf ArrayButton_Click
Now all your buttons are linked to one handler. Course now you need to know which one was clicked. Setting the tag property to ((x - 1) * 10) + y - 1 would be one way
Then you could cast sender to Button. Grab the tag and convert it back to x and y in the handler
Upvotes: 4