[英]Button Added to Visual Studio C# Project Doesn't Recognize Click Event
I am new to Visual Studio and C#. 我是Visual Studio和C#的新手。 I have created my first project -- an inventory application using MySql -- and everything works. 我创建了我的第一个项目-使用MySql的清单应用程序-一切正常。 But I needed to add a new button to the form for deleting records, so I dragged it in from the toolbox, changed the text ("Delete Product") and changed the design name ("DelBtn"). 但是我需要在表单中添加一个新按钮以删除记录,因此我将其从工具箱中拖入,更改了文本(“删除产品”)并更改了设计名称(“ DelBtn”)。 Adding the tool did not create the event handler, so I added the following with the MessageBox for testing: 添加该工具并未创建事件处理程序,因此我在MessageBox中添加了以下内容进行测试:
private void DelBtn_Click(object sender, EventArgs e)
{
MessageBox.Show("Delete button clicked");
}
Clicking the button, however, has no effect, no MessageBox or anything else. 但是,单击该按钮无效,没有MessageBox或其他任何内容。 Can someone help, please? 有人可以帮忙吗?
Go to your form designer, right-click your button, and select "Properties". 转到表单设计器,右键单击您的按钮,然后选择“属性”。 In the properties window, there will be an "Events" button (it looks like a lightning bolt). 在属性窗口中,将有一个“事件”按钮(看起来像是闪电)。 Click that, and look for the Click event of your button. 单击该按钮,然后查找按钮的Click事件。 Make sure that DelBtn_Click
is listed there. 确保DelBtn_Click
列出了DelBtn_Click
。 At that point, the button should respond with your code. 此时,该按钮应以您的代码响应。
Hope this helps! 希望这可以帮助!
You need to make sure you've wired up the handler correctly. 您需要确保正确连接了处理程序。 Look for the designer file that gets generated along with your form (I'm making the assumption that this is winforms). 寻找与您的表单一起生成的设计器文件(我假设这是winforms)。
Then you'll need a line like this in your InitializeComponent
method: 然后,在InitializeComponent
方法中需要这样的一行:
this.button1.Click += new System.EventHandler(this.DelBtn_Click);
You don't have to do this in your designer file, you can do it anywhere you want that runs before your form is shown, eg the constructor. 你不必为此在你的设计师文件,你可以做任何你想要运行显示表单之前,如构造函数。 But it seems to me like something you did -- maybe by manually-renaming your event handler method -- has mangled the automatically-generated designer code that I mentioned above, and you probably just need to fix what's there already. 但是在我看来,您所做的事情-可能是通过手动重新命名事件处理程序方法-弄乱了我上面提到的自动生成的设计器代码,您可能只需要修复已经存在的内容。
如果需要,您需要在designer.cs中委派事件:
DelBtn.Click += new System.EventHandler(this.DelBtn_Click);
Just adding a method with the same name as VS would have made for you won't bind the event to that method. 只需添加与VS相同名称的方法,就不会将事件绑定到该方法。 Open the properties for the button and at the top of the resulting panel you'll see a lightning bolt. 打开按钮的属性,在结果面板的顶部,您将看到一个闪电。 Click that and find the event you want and type in your method name there. 单击该按钮,找到所需的事件,然后在其中键入您的方法名称。
You need to go on the form, click on the button, on the properties menu you need to click on event,find onclick and double click the blank space. 您需要进入表格,单击按钮,在属性菜单上单击事件,找到onclick并双击空白。 It will automatically create the onclick method for you. 它将自动为您创建onclick方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.