Reputation: 33
I have a sudoku game, where I want to autogenerate a sudoku, so that the player can solve it.
When I have generated the numbers in the 2d-Array, which I already did, I want to make these values visible to the user by putting them onto the text-property of 81 buttons. The buttons are all named in a similar manner. (sudoku_0_0, sudoku_0_1 etc.- The first number is the x-Coordinate and the second number is the y-Coordinate)
Can I put values on the buttons with a loop? Like:
for(var x=0;x<=8;x++){
for(var y=0;y<=8;y++){
sudoku_x_y.Text = "Hello"
}
}
Obviously this doesn't work, but is there a way to do something similar to this?
Upvotes: 0
Views: 76
Reputation: 727097
Put your buttons into an array as well.
Make a 2D array of buttons, then do this:
Button[][] sudokuButtons = new Button[9][9];
for (int row = 0 ; row != 9 ; row++) {
for (int col = 0 ; col != 9 ; col++) {
var name = string.Format("sudoku_{0}_{1}", row, col);
sudokuButtons[row][col] = this.Controls.Find(name, true).FirstOrDefault() as Button;
}
}
Upvotes: 4