如何在Visual Studio 2013中随机播放两个按钮? [英] How do I shuffle two buttons in Visual Studio 2013?

查看:105
本文介绍了如何在Visual Studio 2013中随机播放两个按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正试图找出如何在Visual Studio 2013中随机播放两个按钮。

我创建了一些按钮,当我点击一个,我得到这个:



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 :

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





我不知道该把什么放进去用另一个按钮洗牌。

可以有人请帮助我?



必须在Csharp。



我忘记了提到它是在Windows窗体中。



我的意思是通过改变位置改变。按钮1将出现在按钮2所在的位置,按钮2将出现在按钮1所在的位置。这就是主意。



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.

推荐答案

如果你的意思是当你点击一个按钮时它会禁用它,并启用一个不同的按钮,反之亦然(唯一的意思是我可以想想用按钮洗牌然后需要你有两个事件处理程序:每个按钮一个 - 或者至少,将同一个处理程序挂钩到两个按钮。



单击应用程序中的按钮时,它会创建一个事件(在这种情况下为Click事件),这会导致您在显示的方法中编写的代码以上待执行。代码不会在任何其他时间运行,当然也不会在Visual Studio中运行(除非你开始做更复杂的事情)。



所以添加一些代码到方法:

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!");
}

按F5运行你的应用程序。

假设它编译(不保证)那么你会看到你的表单,但你的按钮出现了在你面前,单击Button1将导致显示消息。

如果它没有编译,那么你将在Visual Studio的错误列表面板中找到问题列表。 br />


要在代码中切换按钮的启用状态非常简单:

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;
}

当你运行你的应用程序时,按钮会启用和禁用对方。



如果那不是你的话请问,然后你需要给我们更详细的信息!





我的课程涵盖了他们,但主要是C,还没有在C#中。





它们在C#中非常相似,但语法略有不同。

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];



构建一个(基本)棋盘,你可以使用100以上的值作为红色,而下面的则表示黑色。为一个棋子添加1,为车辆添加2,依此类推,你有基本的棋盘。



在你的情况下,你很难知道建议你做什么游戏开启 - 但是如果你使用16个按钮那么




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;





现在,当你得到一个按钮Click事件时,你可以将它们全部发送到与 sender 参数告诉您按下了哪个按钮:



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)
            {
            ...
            }    
        }

然后你可以使用嵌套的 for 循环来查找myBoard数组中按钮的当前位置作为x和y坐标。



这让你环顾用户点击的按钮,找到 null 单元格,它告诉你需要移动哪个。



我写了一个查看myBoard并设置每个按钮的Location属性的方法(小心不要尝试移动 null )根据它的X和Y位置。



这有意义吗?

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?


这篇关于如何在Visual Studio 2013中随机播放两个按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆