随机修改按钮位置
Modifying randomly button location
我试图在鼠标悬停时随机更改按钮的位置。为此,我使用了以下源代码:
private int modifX()
{
int rdmx;
int x_max = this.Width;
Random rdm = new Random();
rdmx = rdm.Next(0, x_max);
return rdmx;
}
private int ModifY(){
// same with y_max = this.Height;
}
private void bt_win_MouseEnter(object sender, EventArgs e)
{
bt_win.Location = new Point(modifX(), modifY());
}
问题是我的按钮位置总是在一条直线上,如that
我该如何解决?我尝试使用 bt_win.Location.X = modifX();在 mouseEnter 事件上 但似乎我无法处理 Location.X 或 Location.Y
我真的不明白我做错了什么,任何人都有想法并且可以解释我做错了什么?
您需要使用相同的随机实例class。
当您紧密创建 Random class 的两个实例时,它们可以共享相同的种子。所以会生成相同的数字。
private Random _rdm = new Random();
private int modifX()
{
int x_max = this.Width;
int rdmx = _rdm.Next(0, x_max);
return rdmx;
}
private int ModifY(){
// same with y_max = this.Height;
}
private void bt_win_MouseEnter(object sender, EventArgs e)
{
bt_win.Location = new Point(modifX(), modifY());
}
我试图在鼠标悬停时随机更改按钮的位置。为此,我使用了以下源代码:
private int modifX()
{
int rdmx;
int x_max = this.Width;
Random rdm = new Random();
rdmx = rdm.Next(0, x_max);
return rdmx;
}
private int ModifY(){
// same with y_max = this.Height;
}
private void bt_win_MouseEnter(object sender, EventArgs e)
{
bt_win.Location = new Point(modifX(), modifY());
}
问题是我的按钮位置总是在一条直线上,如that
我该如何解决?我尝试使用 bt_win.Location.X = modifX();在 mouseEnter 事件上 但似乎我无法处理 Location.X 或 Location.Y 我真的不明白我做错了什么,任何人都有想法并且可以解释我做错了什么?
您需要使用相同的随机实例class。 当您紧密创建 Random class 的两个实例时,它们可以共享相同的种子。所以会生成相同的数字。
private Random _rdm = new Random();
private int modifX()
{
int x_max = this.Width;
int rdmx = _rdm.Next(0, x_max);
return rdmx;
}
private int ModifY(){
// same with y_max = this.Height;
}
private void bt_win_MouseEnter(object sender, EventArgs e)
{
bt_win.Location = new Point(modifX(), modifY());
}