Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi,

I'm trying to find out how to shuffle two buttons in Visual Studio 2013.
I created a few buttons and when I click on one, I get this :

C#
private void button1_Click(object sender, EventArgs e)
        {
            
        }


I don't know what to put inside to shuffle with another button.
Can someone please be kind enough to help me?

It has to be in Csharp.

I forgot to mention that it is in Windows Forms.

I mean by shuffling to change places. Button 1 will appear where button 2 was, and button 2 will appear where button 1 was. That's the idea.
Posted
Updated 2-Mar-15 6:01am
v6
Comments
Sergey Alexandrovich Kryukov 2-Mar-15 11:43am    
Visual Studio is just IDE, it does not have those buttons.
What have you tried so far?
Also, don't forget, if you mention "Button" type, the question will be: which one. Full type name, please.
—SA
Member 11492079 2-Mar-15 12:00pm    
I'm sorry I don't understand why I have to put which one. I'm new to Csharp and to this website.
Sergey Alexandrovich Kryukov 2-Mar-15 13:39pm    
What do you need to understand. There is no such type as just "Button". There are different types using this simple type name. Which one do you mean?
Do you at least understand what is type name? If you do, please mention the particular name (full). If not, you are not yet ready to do any development.
—SA
ZurdoDev 2-Mar-15 11:50am    
What do you mean by shuffle?
Sergey Alexandrovich Kryukov 2-Mar-15 13:40pm    
A valid question. It could be swapping the locations, the title, or combination of something...
—SA

1 solution

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:
C#
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:
C#
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.
C#
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

C#
Button[,] myBoard = new Button[4,4];

would declare it, and then you could just fill the board:
C#
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:
C#
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?
 
Share this answer
 
v2
Comments
Member 11492079 2-Mar-15 12:08pm    
Hi, I edited my post because I wasn't being clear enough. I actually want to do a Csharp version of this : https://www.youtube.com/watch?v=JqY-PWRyseQ . I think it can be possible.
OriginalGriff 2-Mar-15 12:19pm    
That's easy! :laugh:

Point temp = button1.Location
button1.Location = button2.Location;
button2.Location = temp;

The only slight complexity is in working out which buttons are button1 and button2 - and the best way to handle that is via a 2D array - I assume you are using one for your app allready?
Member 11492079 2-Mar-15 12:23pm    
Thank you, I'm not using one for the app and I don't really know how to use them.
OriginalGriff 2-Mar-15 12:37pm    
That's going to be a problem for you - it's a lot easier to work out where a button is (and you need to known that to work out which way it can move when the user clicks it) if you use an array.

Has your course covered them yet, at all? If not, you might be better waiting a while until it does - as it gets really unpleasant to do without them!
Member 11492079 2-Mar-15 13:17pm    
My course has covered them but mainly in C, not yet in C#.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900