I've started making a Minesweeper game in vb.net using a dynamically created grid of buttons, which are stored in a 2D array. I have another 2D array (Boolean
), that keeps track of whether a certain position is covered or uncovered. And I have an 'update grid' method that can be ran whenever, to update the grid, so that all that should be uncovered is uncovered on the screen.
How would I make it so that when any button is clicked, that button's state is set to uncovered? I tried using an event handler, but I couldn't pass any arguments, and I need to know the button's x and y position, so I can uncover the correct button.
Thanks.
You could put all your buttons in a Dictionary(Of Button, Boolean)
each with the value True
. Later in your Click event handler cast the sender to Button and set the corresponding entry in the dictionary to False
.
Somewhat like this (excuse my bad VB, I'm normally using C#):
Buttons = new Dictionary(Of Button, Boolean)()
For Each button in YourButtonArray
Buttons.Add(button, True)
End
' Click handler:
Dim clickedButton = CType(sender, Button)
Buttons(clickedButton) = False
Update: Alternatively -- as @Hans Passant pointed out in the comments to your question -- you can use the Tag
property, which would be simpler.