随机按钮外观不起作用
Random Button Appearance not Working
警告,我是 C# 的新手。无论如何,我有一个随机生成器,它会选择一个数字,如果选择了数字 x,那么我的 x 按钮就会出现并重复。然而,这有时有效,有时无效。我的意思是这个按钮应该 button1.Visible = true 但是当我点击另一个按钮按钮 1 应该消失而另一个按钮必须出现时,我需要它在只有一个可见按钮的情况下工作,但有时一个按钮根本不可见。很奇怪。
这里是一键点击:
private void A_Click(object sender, EventArgs e)
{
if (ao.Visible == true)
{
ao.Visible = false;
Random rnd = new Random();
int y = rnd.Next(1, 7);
if (y == 1)
{
eo.Visible = true;
}
if (y == 2)
{
ao.Visible = true;
}
if (y == 4)
{
dd.Visible = true;
}
if (y == 5)
{
go.Visible = true;
}
if (y == 6)
{
eeo.Visible = true;
}
// timer1.Stop();
timer1.Start();
label1.Text = "Correct";
}
else
{
label1.Text = "Incorrect";
}
按钮 A 可见,它将转到我刚刚粘贴的那个事件,并使其不可见,另一个可见。有时什么也看不见。
不太确定您的代码中发生的所有事情,但您似乎希望一次只显示一个按钮。如果是这样,请执行以下操作:
public partial class Form1 : Form
{
private Random rnd = new Random();
private List<Button> buttons = new List<Button>();
public Form1()
{
InitializeComponent();
buttons.Add(ao);
buttons.Add(eo);
buttons.Add(dd);
buttons.Add(go);
buttons.Add(eeo);
}
private void A_Click(object sender, EventArgs e)
{
int y = rnd.Next(buttons.Count);
for (int i = 0; i < buttons.Count; i++ )
{
buttons[i].Visible = (i == y);
}
}
}
在您的描述中,您说您希望按钮再次消失,但您再也不会将 Visible
设置为 false。在这种情况下,我实际上不会使用开关。
class RandomButtonForm : Form
{
private Random rng;
private List<Button> buttons;
public RandomButtonForm()
{
this.rng = new Random();
this.buttons = new List<Button>();
this.AddButton(10, 10, "Button 1");
this.AddButton(110, 10, "Button 2");
this.AddButton(210, 10, "Button 3");
}
public AddButton(int x, int y, string text)
{
Button button = new Button();
button.Visible = false;
button.X = x;
button.Y = y;
button.Text = text;
this.buttons.Add(button);
this.Controls.Add(button);
}
private void A_Click(object sender, EventArgs e)
{
int r = this.rng.Next(this.buttons.Count);
for (int b = 0; b < this.buttons.Count; b++)
{
this.buttons[b].Visible = (b == r);
}
}
}
相反,这实际上使用直接比较来为每个按钮提供一个布尔值。一个简单的例子是:
ao.Visible = (y == 1);
// (y == 1) is either true or false.
这意味着按钮不仅在值为 1 时显示,而且在值不是 1 时隐藏,允许您反复按 "go" 按钮。
此示例还包括一些其他有用的东西,例如 List<Button>
并自动为您的随机数使用它的计数,如果您需要更改按钮的数量,可以更轻松地维护它。
警告,我是 C# 的新手。无论如何,我有一个随机生成器,它会选择一个数字,如果选择了数字 x,那么我的 x 按钮就会出现并重复。然而,这有时有效,有时无效。我的意思是这个按钮应该 button1.Visible = true 但是当我点击另一个按钮按钮 1 应该消失而另一个按钮必须出现时,我需要它在只有一个可见按钮的情况下工作,但有时一个按钮根本不可见。很奇怪。
这里是一键点击:
private void A_Click(object sender, EventArgs e)
{
if (ao.Visible == true)
{
ao.Visible = false;
Random rnd = new Random();
int y = rnd.Next(1, 7);
if (y == 1)
{
eo.Visible = true;
}
if (y == 2)
{
ao.Visible = true;
}
if (y == 4)
{
dd.Visible = true;
}
if (y == 5)
{
go.Visible = true;
}
if (y == 6)
{
eeo.Visible = true;
}
// timer1.Stop();
timer1.Start();
label1.Text = "Correct";
}
else
{
label1.Text = "Incorrect";
}
按钮 A 可见,它将转到我刚刚粘贴的那个事件,并使其不可见,另一个可见。有时什么也看不见。
不太确定您的代码中发生的所有事情,但您似乎希望一次只显示一个按钮。如果是这样,请执行以下操作:
public partial class Form1 : Form
{
private Random rnd = new Random();
private List<Button> buttons = new List<Button>();
public Form1()
{
InitializeComponent();
buttons.Add(ao);
buttons.Add(eo);
buttons.Add(dd);
buttons.Add(go);
buttons.Add(eeo);
}
private void A_Click(object sender, EventArgs e)
{
int y = rnd.Next(buttons.Count);
for (int i = 0; i < buttons.Count; i++ )
{
buttons[i].Visible = (i == y);
}
}
}
在您的描述中,您说您希望按钮再次消失,但您再也不会将 Visible
设置为 false。在这种情况下,我实际上不会使用开关。
class RandomButtonForm : Form
{
private Random rng;
private List<Button> buttons;
public RandomButtonForm()
{
this.rng = new Random();
this.buttons = new List<Button>();
this.AddButton(10, 10, "Button 1");
this.AddButton(110, 10, "Button 2");
this.AddButton(210, 10, "Button 3");
}
public AddButton(int x, int y, string text)
{
Button button = new Button();
button.Visible = false;
button.X = x;
button.Y = y;
button.Text = text;
this.buttons.Add(button);
this.Controls.Add(button);
}
private void A_Click(object sender, EventArgs e)
{
int r = this.rng.Next(this.buttons.Count);
for (int b = 0; b < this.buttons.Count; b++)
{
this.buttons[b].Visible = (b == r);
}
}
}
相反,这实际上使用直接比较来为每个按钮提供一个布尔值。一个简单的例子是:
ao.Visible = (y == 1);
// (y == 1) is either true or false.
这意味着按钮不仅在值为 1 时显示,而且在值不是 1 时隐藏,允许您反复按 "go" 按钮。
此示例还包括一些其他有用的东西,例如 List<Button>
并自动为您的随机数使用它的计数,如果您需要更改按钮的数量,可以更轻松地维护它。