如何在C#上的设计时创建事件? [英] How to create events on Design time on C#?

查看:131
本文介绍了如何在C#上的设计时创建事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在设计时创建按键事件.我怎样才能做到这一点???我想创建一个事件,应该在不开始调试的情况下进行处理.我的意思是我创建了一个用户控件,当我按F1键时,它将在Form1 [Design]上打开有关该控件的帮助文件.

解决方案

取决于:如果您正在谈论向类中添加类的用户可以处理的事件,那么这很简单-只需添加代码即可:

 ///  <  > 
 /// 表示[Description]的事件
 ///  <  /summary  > 
 公共 事件 EventHandler名称;
///  <  > 
 /// 发出信号以通知订户已出现[描述]
 ///  <  /summary  > 
 ///  <  参数   名称="e"  <  > 
 受保护的 虚拟的 无效 OnName (EventArgs e)
    {
    EventHandler eh =名称;
    如果(嗯!= )
        {
        eh(,e);
        }
    }


如果要在现有事件中添加处理程序,则只需在设计器中突出显示该对象,然后:
查看属性"窗格.
按下事件"按钮-看起来像是闪电.
找到您要处理的事件.
双击它.

这将添加处理程序,并为您添加一个基本的处理程序例程.

如果您经常要创建事件,那么可能值得一看:如何使用按键事件 [ ^ ].
键盘事件处理:onkeypress
[ ^ ].


i must create a keypress event on design time. how can i do this??? i want to make an event which should be handled without starting debugging. i mean i created an user control and when i press F1 key, it will open a help file about the control on Form1[Design].

解决方案

Depends: if you are talking about adding an event to your class that users of your class can handle, then it''s pretty easy - just add the code:

/// <summary>
/// Event to indicate [Description]
/// </summary>
public event EventHandler Name;
/// <summary>
/// Called to signal to subscribers that [Description] occured
/// </summary>
/// <param name="e"></param>
protected virtual void OnName(EventArgs e)
    {
    EventHandler eh = Name;
    if (eh != null)
        {
        eh(this, e);
        }
    }


If you are talking about adding a handler to an existing event, then just highlight the object in the designer and:
Look at the Properties pane.
Press the Events button - it looks like a lightening bolt.
Find the event you want to handle.
Double click it.

That will add the handler, and a bare-bones handler routine for you to fill in.

If you are often going to create events it might be worth your looking at this: A simple code snippet to add an event[^]
It makes it a lot easier to generate the above code!


Please go through the links below to know the implementation of KeyPress Event.
how to use keypress events[^].
Keyboard Event Handling: onkeypress
[^].


这篇关于如何在C#上的设计时创建事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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