Microsoft Visual Studio 和 C#:如何以可视方式向控件添加事件? [英] Microsoft Visual Studio and C#: How to Visually Add Events to Controls?

查看:17
本文介绍了Microsoft Visual Studio 和 C#:如何以可视方式向控件添加事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 C# 在 Microsoft Visual Studio (2008) 的表单中向控件添加事件处理程序?

How would one go about adding an event handler to a control in a form in Microsoft Visual Studio (2008) with C#?

我可以手动完成,但是打开Form的Designer.cs文件,却找不到通过界面完成的方法.

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.

在 MSVC 6 中,使用 C++ 和 MFC,您可以右键单击或执行其他操作,然后找到该控件的所有可能事件的列表.然后你选择它,它会弹出一个窗口,让你创建一个与该事件关联的方法.在 VB 中,它更容易,您可以在代码中完成,它会在方法下拉列表中列出所有事件.

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.

但是,我在 2008 年用 C# 找不到任何类似的东西.Intellisense 有助于填写正确的方法信息.然而,它总是在 Designer.cs 文件中创建方法,而不是在它应该在的部分类的另一半.

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.

推荐答案

在设计器中,单击感兴趣的控件.

In the designer, click the control of interest.

在属性窗口中(按 F4 调出),应该有一个小闪电图标.单击该图标可转到该控件的可用事件列表.双击事件名称将连接一个存根处理程序并将您带到它的代码隐藏处.

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.

精明的读者会看到张贴的屏幕截图是针对网络应用程序 (whups),但对于 winforms 应用程序来说完全相同.

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.

这篇关于Microsoft Visual Studio 和 C#:如何以可视方式向控件添加事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆