简体   繁体   English

Visual Studio中的事件处理程序

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

相关问题 Visual Studio 2019 自动事件处理程序生成 - Visual Studio 2019 Automatic Event Handlers Generation Visual Studio继续创建已经定义的事件处理程序 - Visual Studio keeps creating event handlers that are already defined Visual Studio“查找所有引用”是否对事件处理程序失败? - Visual Studio 'find all references' fails for event handlers? 有没有办法在 Visual Studio 调试器中查看 TextBox TextChanged 事件的所有事件处理程序? - Is there any way to view all event handlers of TextBox TextChanged event in Visual Studio debugger? Visual Studio的C#自动事件处理程序生成。 为什么要创建一个新的委托? - Visual Studio's C# automated event handlers generation. Why does it create a new delegate? 为什么双击时Visual Studio中的HTML按钮不会创建事件处理程序? - Why are my HTML buttons in Visual Studio not creating Event Handlers when double clicked? 默认的Visual Studio始终在同一文件-Form1.cs中创建事件处理程序,如何更改位置? - Default visual studio always create event handlers in the same file - Form1.cs, how to change location? 为什么在Visual Studio中自动创建事件处理程序时会使用对象发送器 - Why do event handlers use Object Sender when being automatically created in Visual Studio SelectedIndexChanged事件在Visual Studio中不可用? - SelectedIndexChanged Event not available in Visual Studio? Visual Studio项目创建的事件 - Event on Visual Studio project creation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM