[英]WinForms button click event not firing
I don't get any errors from Visual Studio so I assumed that the code was right. 我没有从Visual Studio得到任何错误,所以我认为代码是正确的。 When I run the code, I press the button (
button1
), but nothing happens. 运行代码时,我按下了按钮(
button1
),但是什么也没有发生。
private void button1_Click(object sender, EventArgs e)
{
int a = 0;
int b = 1;
int c = 1;
listBox1.Text += a.ToString();
listBox1.Text += b.ToString();
for (int i = 0; i < 20; i++)
{
c = b;
b = a + b;
a = c;
listBox1.Text += b.ToString();
}
}
Listbox's Text
property won't work in this case, since it is used only to set or get the selected item in a listbox, add to the itemsource on each number, then it will work 在这种情况下,列表框的
Text
属性将不起作用,因为它仅用于设置或获取列表框中的选定项,将其添加到每个数字的itemsource中,然后它将起作用
private void button1_Click(object sender, EventArgs e)
{
int a = 0;
int b = 1;
int c = 1;
StringBuilder finalstring = new StringBuilder();
listBox1.Text += a.ToString();
listBox1.Text += b.ToString();
for (int i = 0; i < 20; i++)
{
c = b;
b = a + b;
a = c;
listBox1.Items.Add(b);
}
}
Does button
have button1_Click
registered as an event? button
是否已将button1_Click
注册为事件?
You can do it via your code by adding button1.Click += button1_Click
, or double clicking it in your form designer. 您可以通过添加
button1.Click += button1_Click
或在表单设计器中双击它来通过代码来button1.Click += button1_Click
。
If this still does not work, put a breakpoint on the code to see if it is executed. 如果仍然不起作用,请在代码上放置一个断点以查看其是否已执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.