If you mean that when you click on one button it disables it, and enables a different button, and vice versa (the only meaning I can think of for "shuffle" with buttons) then that needs you to have two event handlers: one for each button - or at least, to hook the same handler up to both buttons.
When you click on a button in your application, it creates an
event
(the Click event in this case) which causes the code you wrote inside the method you show above to be executed. The code does not run at any other time, and certainly not inside Visual Studio (except when you start doing a lot more complicated things).
So add some code to the method:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hello! You clicked my button!");
}
And run your app by pressing F5.
Assuming it compiles (not guaranteed) then you will see the form you but your button(s) on appear in front of you, and clicking Button1 will cause the message to be displayed.
If it didn't compile, then you will get a list of the problem in the Error List panel of Visual Studio.
To toggle the enable state of the buttons in code is pretty simple:
private void button1_Click(object sender, EventArgs e)
{
button1.enable = false;
button2.enable = true;
}
private void button2_Click(object sender, EventArgs e)
{
button2.enable = false;
button1.enable = true;
}
and when you run your app the buttons will enable and disable each other.
If that isn;t what you are asking, then you need to give us a lot more detail!
"My course has covered them but mainly in C, not yet in C#."
They are very similar in C#, but the syntax is slightly different.
int[,] myBoard = new int[8,8];
Would construct a (basic) chessboard, where you could use values above 100 for red, and below for black say. Add 1 for a pawn, 2 for a rook, and so on and you have the basic board.
In your case it's difficult to know what to suggest you base the game on - but if you are using 16 buttons then
Button[,] myBoard = new Button[4,4];
would declare it, and then you could just fill the board:
myBoard[0,0] = button1;
myBoard[1,0] = button2;
myBoard[2,0] = button3;
myBoard[3,0] = button4;
myBoard[0,1] = button5;
myBoard[1,1] = button6;
myBoard[2,1] = button7;
myBoard[3,1] = button8;
myBoard[0,2] = button9;
myBoard[1,2] = button10;
myBoard[2,2] = button11;
myBoard[3,2] = button12;
myBoard[0,3] = button13;
myBoard[1,3] = button14;
myBoard[2,3] = button15;
myBoard[3,3] = null;
Now, when you get a button Click event, you can send them all to the same handler method as the
sender
parameter tells you which button was pressed:
private void AnyShufflePuzzleSquare_Click(object sender, EventArgs e)
{
Button clicked = sender as Button;
if (clicked != null)
{
...
}
}
You can then use nested
for
loops to find the current location of the button in your myBoard array as an x and y coordinate.
That lets you "look round" the button the user clicked to find the
null
cell, and that tells you which you need to move.
I'd write a method that looked at myBoard and set the Location property of each button (and being careful not to try and move the
null
) according to it's X and Y location.
Does that make sense?