简体   繁体   English

添加到Visual Studio C#项目中的按钮无法识别Click事件

[英]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.

相关问题 动态添加的链接按钮不会在首次点击时触发点击事件-C# - Dynamically added Link Button doesn't fire the on click event on the first click - C# C#Visual Studio无法识别ZipFile.ExtractToDirectory - C# Visual Studio doesn't recognize ZipFile.ExtractToDirectory 如何在Visual Studio C#中禁用和启用按钮上的click事件 - How to disable and enable a click event on a button in Visual Studio C# Visual Studio 无法识别同一项目中的命名空间 - Visual studio doesn't recognize namespace inside same project Visual Studio 2013 C# 编辑器无法识别我的课程 - Visual Studio 2013 C# Editor doesn't recognize my class 如何在Visual Studio C#中向我自己的按钮添加按钮单击事件 - How do I add a button click event to my own button in Visual Studio C# 将按钮单击事件功能移动到visual studio中的其他类? - C# - Move button click event function in to other class in visual studio? - C# Visual Studio无法识别我的构造函数? - Visual studio doesn't recognize my constructor? 视觉工作室突然什么也没认出来 - visual studio suddenly doesn't recognize anything 当我点击右上角的 X 按钮时程序没有关闭(Visual Studio、Forms、C#) - Program doesn't close when I hit the X button top right (Visual Studio, Forms, C#)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM