许多控件的事件处理程序 [英] Event Handler for Many Controls

查看:70
本文介绍了许多控件的事件处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表格,其中可能包含同一控件的多个实例,具体取决于应用程序.一些应用程序将需要少数几个,而其他应用程序将需要4​​0个以上的控件.在VB6中,我将手动将控件添加到表单中,分配一个有意义的名称,然后双击该控件以实现事件处理程序.控件之间的事件处理程序将有很大的不同.

我不熟悉C#中的事件,但我认为我将为每个控件实现事件的方式如下:

I have a form that may contain several instances of the same control depending on the application. Some applications will need a handful, others will need 40+ of these controls. In VB6 I would add controls to the form manually, assign a meaningful name and then double click the control to implement an event handler. The event handler will be very different from control to control.

I am new to doing events in C# but I think the way I would implement the event for each control is as follows:

Control1.Operate += new MyControl.OperateEventHandler(Control1_Operate);
.
.
.
Control10.Operate += new MyControl.OperateEventHandler(Control10_Operate);



然后,我将在Control1_Operate中实现代码,依此类推.

但是我宁愿不必在每次向屏幕添加控件时都执行上述操作.我希望C#像下面那样为我处理它,但是当我需要提供实际的事件处理程序时,我就陷入了困境.



Then I would implement the code in Control1_Operate and so on.

But I would rather not have to do the above every time I add a control to the screen. I would like C# to handle it for me like below but I get stuck when I need to provide the actual event handler.

foreach (Control ctrl in this.Controls)
{
    if (ctrl.GetType() == typeof(UniControl))
    {
        temp = (UniControl)ctrl;
        temp.Operate += new UniControl.OperateEventHandler(Generic_Operate);
    }
}



如果我使用Generic_Operate处理程序,那么我将需要以某种方式引用需要操作的控件.还是可以通过某种方式自动生成处理程序名称":



If I use a Generic_Operate handler then I will somehow need to a have reference to the control that needs to be operated. Or can I somehow auto generate the handler "name" like this:

foreach (Control ctrl in this.Controls)
{
    if (ctrl.GetType() == typeof(UniControl))
    {
        temp = (UniControl)ctrl;
        temp.Operate += new UniControl.OperateEventHandler(temp.Name + "_Operate");
    }
}



显然上述方法行不通,但也许有一种方法可以达到我想要的效果?



Obviously the above won''t work but maybe there''s a way to get the effect I want?

推荐答案

事件处理程序具有sender属性,您可以用于获取已触发的控件.

如果所有事件处理程序都相似,则只需使用一个处理程序并使用sender属性.
Event handlers have a sender property which you can use to get the control that fired.

If all your event handlers are similar just use one handler and use the sender property.


取决于Operate事件处理程序的委托定义.事件处理程序应接受一个参数,该参数包含对引发事件的控件的引用.如果控件是您自己的,则可以查看源代码,并查看是否正确定义了事件委托,以允许将EventArgument或其派生参数作为参数传递给事件处理程序.如果您没有控件的源代码,并且事件处理程序委托没有参数,则将无法使用.

问候,

—MRB
Depends on the delegate definition for your Operate event handler. The event handler should accept a parameter which would contain a reference to the control that raised the event. If the control is your own you can take a look at the source code and see if the event delegate was properly defined to allow the passing of an EventArgument or a derivat thereof as a parameter to the event handler. If you don''t have the source code of the control and the event handler delegate does not have a parameter this won''t work.

Regards,

—MRB


事件处理程序传递事件源作为其参数之一.内置类型均将源(按.Net约定发送者)作为对象的第一个参数传递.这看起来像是一个自定义委托,因此请确保将事件的源作为第一个参数传递–您可以使其像内置类型一样成为对象,或者,如果仅对一种类型的对象有意义,则可以使用该类型,即

The event handler should pass the source of the event as one of its parameters. The built in types all pass the source (sender, by .Net convention) as the first argument as an object. This looks like a custom delegate, so make sure it passes the source of the event as the first argument – you can either make it object like the built-in types or, if it only makes sense on one type of object, you can use that type, i.e.

class MyControl {
 delegate void SomeSpecialistEventHandler(MyControl sender, int magicNumber, Color favouriteColour);
 // ...
}



然后,在处理程序中,您可以使用第一个参数来找出您触发了哪个控件:



Then, in the handler, you can use the first parameter to find out which control you were fired on:

void MySpecialistEventHandler(MyControl sender, int magicNumber, Color favouriteColour){
 // Use ''sender'' when you need the object that triggered the event
}

void NormalButton_Click(object sender, EventArgs e){
 Button button = sender as Button;
 // Use ''button'' as the typed source
 // If this event can be fired by different types, put a guard in, e.g.
 //  if(button == null) return;
 // but generally you shouldn''t make that possible.
}


这篇关于许多控件的事件处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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