简体   繁体   English

在 C# 中按下按钮时如何更改按钮的位置?

[英]How could I change the location of a button whenever it is pressed in C#?

I am currently trying to make a game in C#, and one of the features is a minigame in which you have to press a button which changes location upon click as many times as you can.我目前正在尝试在 C# 中制作游戏,其中一个功能是迷你游戏,您必须在其中按下一个按钮,该按钮会在点击后尽可能多地改变位置。

How could I randomise the location of it between (12, 105) and (220, 177) ?我如何在(12, 105)(220, 177)之间随机化它的位置? I'm using Visual Studio 2022.我正在使用 Visual Studio 2022。

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

Assuming that you use WinForms , you can put it as follow:假设你使用WinForms ,你可以这样说:

 static readonly Random s_Random = new Random();

 private void button1_Click(object sender, EventArgs e) {
   clicks++;

   button1.Location = new Point(
     s_Random.Next(12, 220 + 1), // x (left) in [12..220] range
     s_Random.Next(105, 177 + 1) // y (top) in [105..177] range
   ); 
 }
  1. In order to generate random values, you can use the Random class .为了生成随机值,您可以使用Random class
    As you can see in the documentation above, in order to get an integer value between a and b, you need to use:正如您在上面的文档中看到的,为了获得 a 和 b 之间的 integer 值,您需要使用:
Random rand = new Random();
int val = rand.Next(a, b+1);
  1. In order to move a button, you can modify it's Left , and Top properies.为了移动按钮,您可以修改它的LeftTop属性。 This applies to WinForms, but WPF has similar properties.这适用于 WinForms,但 WPF 具有类似的属性。

The code below demonstrates both:下面的代码演示了两者:

// Can be kept as a class member:
Random rand = new Random(); 

// Note: change x1,x2,y1,y2 below to control the range for locations.
int x1 = 12;
int x2 = 220;
int y1 = 105;
int y2 = 177;
// Randomize location in the defined range:
int x = rand.Next(x1, x2 + 1);
int y = rand.Next(y1, y2 + 1);
// Move the button:
button1.Left = x;
button1.Top = y;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在每次按下按钮时改变其颜色? - How to change colour of button whenever it is pressed? 当按下表单“b”中的按钮时,如何更改表单“a”中的 label 文本 .net C# - How can I change the label text in form "a" when a button is pressed in form "b" .net C# 如何确保在 C# 中没有按下按钮? - How do I make sure that a button is not pressed down in C#? XNA C#-如何在不被按下的情况下按下按钮 - XNA C# - How to make a button pressed without being pressed 每当我单击 C# 中的按钮时,如何将 Append 项添加到另一个 Window 中的列表框? - How Do I Append Items to a Listbox in Another Window Whenever I Click a Button in C#? Windows Phone c#,处于按下状态时如何更改按钮图像? - Windows Phone c#, how to change button image when in pressed state? C#如何在按下退格按钮时清除文本框 - C# How can I clear a textbox when the backspace button is pressed 当在c#winform中按下PageDown键时,如何触发Button Click事件 - How can I fire a Button Click event when PageDown key pressed in c# winform 如何在WP7的c#中自然使代码看起来像被按下然后从代码中取消按下? - How do I make a button seem like pressed and then unpressed from code naturally in c# for WP7? C#+ SlimDX / XInput-如何在按下按钮时进行轮询? - C# + SlimDX / XInput - how to poll when button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM