Perhaps this is a stupid question, but for the life of me I cannot find a way to do this.
也许这是一个愚蠢的问题,但对于我的生活,我找不到办法做到这一点。
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. 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. 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.
我可以手动完成,但打开Form的Designer.cs文件,但我无法通过界面找到方法。在MSVC 6中,使用C ++和MFC,您可以右键单击或执行其他操作,并查找该控件的所有可能事件的列表。然后你选择它,它会弹出一个窗口,让你创建一个与该事件相关联的方法。在VB中它更容易,您可以在代码中执行它并且它列出了方法下拉列表中的所有事件。
However, I cannot find anything of the sort in 2008 with C#. Intellisense helps fill in the correct method information. However, it always creates the method in the Designer.cs file, not the other half of the partial class where it should be.
但是,我在2008年用C#找不到任何类似的东西。 Intellisense有助于填写正确的方法信息。但是,它总是在Designer.cs文件中创建方法,而不是它应该在的部分类的另一半。
Thank you.
2 个解决方案
#1
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. 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.
在属性窗口中(点击F4将其启动),应该有一个小闪电图标。单击该图标可转到该控件的可用事件列表。双击事件名称将连接存根处理程序并将您带到代码隐藏处。
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应用程序来说,这是完全相同的。
#2
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
alt text http://img222.imageshack.us/img222/4456/propv.jpg
#1
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. 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.
在属性窗口中(点击F4将其启动),应该有一个小闪电图标。单击该图标可转到该控件的可用事件列表。双击事件名称将连接存根处理程序并将您带到代码隐藏处。
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应用程序来说,这是完全相同的。
#2
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
alt text http://img222.imageshack.us/img222/4456/propv.jpg