0

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?

1
  • Is this WinForms? WPF? Something else? Commented Mar 24, 2018 at 21:51

1 Answer 1

4

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Or just loop through the Items/Children property of the element that contains all those buttons.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.