Is there a better way to do this? (Select Tile in grid)

So, I have an array of 9 booleans and I need to toggle them depending on a square clicked in a custom canvas class. If a boolean is True then it’s corresponding tile on the canvas is coloured, if not then the tile is grey. I’m fine with the painting of the canvas, which is in a separate method. bValues() is a property of the canvas.

For my purposes, there will always be nine tiles in a 3x3 grid.

I’ve got the following, but the embedded “Select Cases” make me wonder if this is the best approach:

[code]Dim xHit,yHit as Double
xHit = Floor(x/(me.Width/3))
yHit = Floor(y/(me.Height/3))

Select Case yHit
Case 0
Select Case xHit
Case 0
if bValues(0) Then
bValues(0) = False
Else
bValues(0) = True
End
Case 1
if bValues(1) Then
bValues(1) = False
Else
bValues(1) = True
End
Case 2
if bValues(2) Then
bValues(2) = False
Else
bValues(2) = True
End
End Select
Case 1
Select Case xHit
Case 0
if bValues(3) Then
bValues(3) = False
Else
bValues(3) = True
End
Case 1
if bValues(4) Then
bValues(4) = False
Else
bValues(4) = True
End
Case 2
if bValues(5) Then
bValues(5) = False
Else
bValues(5) = True
End
End Select
Case 2
Select Case xHit
Case 0
if bValues(6) Then
bValues(6) = False
Else
bValues(6) = True
End
Case 1
if bValues(7) Then
bValues(7) = False
Else
bValues(7) = True
End
Case 2
if bValues(8) Then
bValues(8) = False
Else
bValues(8) = True
End
End Select
Else
End Select
DrawCells[/code]

You need the index of the array from xHit, yHit and the number of the tiles in a row/column. Something like:

index = xHit + yHit * ( NoOfCols - 1)

Convert yHit and xHit to a 1 dimensional index

Dim index as Integer = yHit*3+xHit
bValues(index) = Not(bValues(index))

Perfect, thanks, both of you