[英]Event handlers in Visual Studio
When I try to create an event handler eg. 当我尝试创建一个事件处理程序,例如。 button click in c#, if I write the code manually in code behind class, the event handler won't be called;
按钮单击c#,如果我在代码后面手动编写代码,则不会调用事件处理程序; Whereas if I double click the button on the form and the VS auto generates the event, it will work perfectly.
然而,如果我双击表单上的按钮并且VS auto生成事件,它将完美地工作。 What is the reason behind this behavior?
这种行为背后的原因是什么?
There is a Form.Designer.cs
file that hooks up the events for each individual form (each form has it's own designer file). 有一个
Form.Designer.cs
文件,用于连接每个表单的事件(每个表单都有自己的设计器文件)。
Open that and you'll find lines like: 打开它,你会发现如下行:
button1.Click += button1_Click;
..etc. ..等等。
As below: 如下:
Your file isnt able to know that the event handler is to be linked to the function you have created. 您的文件无法知道事件处理程序将链接到您创建的函数。
If you want to link manually, click on the button once, and in the top section of the properties window, you'll see a lightning symbol(events). 如果要手动链接,请单击按钮一次,然后在属性窗口的顶部,您将看到闪电符号(事件)。 Click on that, and scroll down to the click event, and over there, add your function.
单击它,向下滚动到单击事件,然后在那里添加您的功能。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.