[已解决]如何为C#控件添加事件 [英] [Solved] How to add Event for C# Control

查看:189
本文介绍了[已解决]如何为C#控件添加事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我有一个关于如何在C#中为控件添加事件的查询.
假设我想在标签控件的Mouse over事件中编写代码.
我双击了该标签,但它打开了click事件.
我也尝试过右上方的事件下拉列表.但没有找到
任何evnet都只包含以下代码中的事件.

请给我建议.

Hi all ,
I have one query regarding how to add event for controls in C#.
Suppose I want to write code in Mouse over event of label control .
I did double clicked on the lable but it opened the click event.
I have also tried the upper right event drop down list. but didn''t found
any evnet it only contain that event which are in code below.

Please suggest me the way.

推荐答案

亲爱的拉胡尔
您有两种方法
方法1)使用属性窗口并选择事件,双击所需事件;它将自动生成代码,因此您可以执行必要的操作
方法2)使用代码处理偶数
在窗口(form designer.cs)中处理必要的事件,即
Dear Rahul
You have two methods
Method 1) Use property window and select events, Double click the desired event; It will autogenerate the code and tthus you perfrom the necessary action
Method 2) Handle the even using code
In the window(form designer.cs) handle the necessary event ie
label1.MouseEnter +=new System.EventHandler(label1_MouseEnter);


现在在cs文件中添加方法


now in the cs file add method

private void label1_MouseEnter(object sender, EventArgs e)


如果要在用户控件上创建自己的事件处理,请参考下面的链接
http://www.akadia.com/services/dotnet_delegates_and_events.html [


If you wan to create your own Event Handiling on a user control then refer below link
http://www.akadia.com/services/dotnet_delegates_and_events.html[^]

Thanks And Regards
Vipin Kumar Mallaya G


在您的Label控件上执行:

On your Label control do:

// Get event, when mouse enter Label control
this.label_HelpText.MouseEnter += new System.EventHandler(this.HelpText_MouseEnter);





// Stop event when mouse enter Label control
this.label_HelpText.MouseEnter -= this.HelpText_MouseEnter;


除了其他答案,我建议始终首选匿名方法,即使是初学者也是如此.由于许多原因,它的支持性要强得多.另外,诸如"label_HelpText_MouseEnter"之类的名称也违反了(好的)Microsoft命名约定. (是的,即使名称是由Microsoft自动生成的;这也不是不重命名的好借口).

因此,请使用:
In addition to other answers, I recommend to always prefer anonymous methods, even for a beginner. By many reasons, it''s much more supportable. Also, names like "label_HelpText_MouseEnter" violate (good) Microsoft naming conventions. (Yes, even though the names are auto-generated by Microsoft; this is not a good excuse not to rename them).

So, use:
myLabel.MouseEnter += (sender, eventArgs) => {
    DoSomething((Label)sender);
};



上面显示的lambda表达式的方便之处在于,您甚至不需要指定参数的类型,因为如果发生事件,则可以从类型中推断出它们.

如果使用C#v.2.0,则lambda表达式不可用.但是,请以显式形式使用匿名委托:



The convenience of lambda expression show above is that you don''t even need to specify types of the argument as they are inferred from the type if the event.

If C# v.2.0 is used, lambda expressions are not available. Nevertheless, use anonymous delegates in explicit form:

myLabel.MouseEnter += delegate(object sender, MouseEventArgs eventArgs) {
    DoSomething((Label)sender);
};



为什么所有这些都更受支持?因为您不必两个事件参数都遵循处理程序的完整签名.看一下DoSomething-仅使用label参数;类型转换是在真实的"匿名处理程序中完成的.通过这种方式,您可以将处理程序与处理程序的语义部分分开.

另外,您可以通过在不同位置添加几个不同的处理程序来使用事件的多播功能.在许多情况下,它非常有用.

可以在事件实际使用之前的任何地方调用上面显示的代码.使用Forms时,我更喜欢使用Form构造函数结尾处调用的单个设置方法来调用它.

—SA



Why all this is more supportable? Because you don''t have to follow full signature of the handler with both event arguments. Look at DoSomething — is uses only the label argument; the type cast is done in a "real" anonymous handler. In this way, you separate a handler from a semantic part of the handler.

Also, you can use multi-cast feature of the event by adding several different handlers in different places. It is very useful in many cases.

The code like the ones shown above can be called anywhere before the event is actually uses. With Forms, I prefer calling it in a single setup method called from the Form constructor at its end.

—SA


这篇关于[已解决]如何为C#控件添加事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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