简体   繁体   English

Microsoft Visual Studio 和 C#:如何以可视化方式向控件添加事件?

[英]Microsoft Visual Studio and C#: How to Visually Add Events to Controls?

How would one go about adding an event handler to a control in a form in Microsoft Visual Studio (2008) with C#?如何使用 C# 在 Microsoft Visual Studio (2008) 的表单中向控件添加事件处理程序?

I can do it manually, but opening the Designer.cs file for the Form, but I cannot find a way to do it through the interface.我可以手动执行此操作,但打开表单的 Designer.cs 文件,但我找不到通过界面执行此操作的方法。

In MSVC 6, with C++ and MFC, you could right click or some other action, and find a list of all the possible events for that control.在 MSVC 6 中,使用 C++ 和 MFC,您可以右键单击或执行其他操作,并找到该控件所有可能事件的列表。 Then you select it and it would bring up window letting you create a method to associate with that event.然后你选择它,它会弹出窗口让你创建一个方法来关联那个事件。 In VB it was even easier, you could do it in the code and it listed all the events in the method drop downs.在 VB 中,它甚至更容易,您可以在代码中完成,它会在方法下拉列表中列出所有事件。

However, I cannot find anything of the sort in 2008 with C#.但是,我无法在 2008 年使用 C# 找到任何此类内容。 Intellisense helps fill in the correct method information. Intellisense 帮助填写正确的方法信息。 However, it always creates the method in the Designer.cs file, not the other half of the partial class where it should be.但是,它总是在 Designer.cs 文件中创建方法,而不是在分部类的另一半中创建方法。

In the designer, click the control of interest.在设计器中,单击感兴趣的控件。

In the properties window (hit F4 to bring it up), there should be a little lightning bolt icon.在属性窗口中(按 F4 调出),应该有一个小闪电图标。 Clicking the icon takes you to the list of available events for that control.单击该图标会将您带到该控件的可用事件列表。 Double-clicking the event name will wire up a stub handler and take you to the code-behind for it.双击事件名称将连接一个存根处理程序并将您带到它的代码隐藏。

替代文字

Edit: the astute reader will see that the posted screenshot is for a web app (whups), but it's the exact same thing for a winforms app.编辑:精明的读者会看到发布的屏幕截图是针对网络应用程序 (whups) 的,但它与 winforms 应用程序完全相同。

All you have to do is to have the form/control open in designer view, have the control in question in focus/selected, open up the properties view and flick over to the events tab.您所要做的就是在设计器视图中打开窗体/控件,将有问题的控件置于焦点/选中状态,打开属性视图并轻弹到事件选项卡。 Double click on the event that you're interested in handling.双击您有兴趣处理的事件。

alt text http://img222.imageshack.us/img222/4456/propv.jpg替代文本 http://img222.imageshack.us/img222/4456/propv.jpg

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM