I want to make a game where the playing pieces can move around. When I click on a playing piece it shows all available moves were I can go like this:

Red represents legal moves and dark gray represents a wall.
I have created a grid and buttons in it. When I click on a button, I subtract or add the available step count to the button index in the grid. For example if my piece can move 3 places, I subtract 3 from the index of the button and get the available position to the left of the piece. Then I do the same for all other directions. It looks something like this:
For each i as button in grid
Select case grid.indexof(i)
Case grid.getindex(currentPlayingPiece) - 3 'Left
i.background = brushes.red
Case grid.getindex(currentPlayingPiece) + 3 'Right
i.background = brushes.red
Case grid.getindex(currentPlayingPiece) - grid.columndefinitions.count 'Top
i.background = brushes.red
...
Next
Using this method it is very slow and cumbersome to get all available moves and not intuitive to get top and bottom moves. Also if there is a wall in the way and behind there’s a available spot it shows that I can move to it. So my question is: is there a better method to accomplish this?
If you prefer not to use an “advanced” algorithm involving graphs or similar solutions, there are a couple of simpler things you might try.
Firstly, creating a variable instead of calling getIndex() a bunch of times might save a few cycles.
Secondly, I would try to use the absolute of each position to evaluate, and so calculate the distance without having to check once for each direction (+ or -); Maybe something like this: