In Winforms I have 72 buttons with images that are supposed toggle back and forth between different images when clicked, but I can’t find why the first click doesn’t make them toggle, all clicks after the first click seem to work as expected.
For each button I use the following code in the Form1_Load
method:
for(int heightIndex = 0; heightIndex < 12; heightIndex++)
{
for(int widthIndex = 0; widthIndex < 6; widthIndex++)
{
allTiles[heightIndex, widthIndex].CausesValidation = false;
allTiles[heightIndex, widthIndex].Click += TileClicked;
}
}
The TileClicked
method looks like this:
private void TileClicked(object sender, EventArgs e)
{
Button tile = sender as Button;
InvertState(tile);
}
And InvertState
looks like this:
private void InvertState(Button tile)
{
if(tile.Image == cat)
{
tile.Image = dog;
}
else
{
tile.Image = cat;
}
}
Why does this code only work after the first click?