UserControl自定义事件 [英] UserControl custom event

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

问题描述

您好,

我正在创建一个由按钮,文本框和面板组成的自定义ComboBox UserControl。我到了加入控件事件的地步,我卡住了。特别是SelectedIndexChanged事件。我不知道如何将此事件添加到我的控件中。我试过了:



Hello,
i am creating a custom ComboBox UserControl formed out of a button, a textbox and a panel. I got to the point where i add the events of the control, and i got stuck. Particularly, SelectedIndexChanged event. I don''t know how to add this event to my control. I tried:

Public Shadows Event SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

AddHandler MyBase.SelectedIndexChanged, AddressOf _Combobox_SelectedIndexChanged

Private Sub _Combobox_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SelectedIndexChanged
        RaiseEvent SelectedIndexChanged(sender, e)
End Sub





但它显然不是正确的方式...这是我得到的:



无法找到事件''SelectedIndexChanged''。 - 对于Sub



''SelectedIndexChanged''不是''System.Windows.Forms.UserControl''的事件。 - 对于第二行代码...



我做错了什么?此外,我可能已将控件设置为继承Combobox并继承其事件,属性和方法,但由于特定原因,我需要它继承UserControl。

谢谢!



but its obviously not the right way... Here is what i get:

Event ''SelectedIndexChanged'' cannot be found. - for the Sub

''SelectedIndexChanged'' is not an event of ''System.Windows.Forms.UserControl''. - for the second line of code...

What am I doing wrong? Also, I might have set the control to inherit Combobox and inherit its events, properties and methods, but for a specific reason I need it to inherit UserControl.
Thank you!

推荐答案

这很简单。你唯一的问题是,你似乎知道如何使用已经声明的事件,处理它们。这意味着,您在不了解事件的情况下了解其用法,否则您将知道如何声明和调用。你试图表现得像一个声明一个偶数的类的用户,而你需要表现得像这样一个类的作者。



所有你需要知道的是:1 )声明一个事件实例作为类/结构的成员(控件类,在您的情况下),2)事件调用。这些是你永远无法做的两件事,只是一些类/结构的用户。并不是说事件的调用只能来自它的声明类,甚至不可能来自派生类。 (许多人问过如何解雇某些事件。根本没有任何意义。如果事件已在库中声明,则无法在其他地方调用:此限制是重要的万无一失的功能。)



您只需重新开始学习活动和代表。请从这里开始: http://msdn.microsoft.com/en-us/library/awbftdfh.aspx [ ^ ]。



一般的模式很简单。首先,您需要有一些事件参数类。使用其中一个可用类或从 System.EventArgs 类派生自己的类,只有在必须将自定义参数传递给事件的处理程序时才需要它。 />
This is simple. Your only problem is that you seemingly know how to use the already declared events, handle them. It means, you know the usage without understanding of events, otherwise you would know how to declare and invoke then. You try to act like a user of the class declaring an even, while you need to act like an author of such class.

All you need to know is: 1) declaration of an event instance as a member of class/structure (control class, in your case), 2) event invocation. Those are two items you never could do as a mere user of some class/structure. Not that the invocation of the event is only possible from its declaring class, impossible even from the derived class. (Many asked how to fire some certain event. It makes no sense at all. If event is already declared in a library, it cannot be invoked elsewhere: this limitation is the important fool-proof feature.)

You just need to start over about learning the events and perhaps delegates. Please start here: http://msdn.microsoft.com/en-us/library/awbftdfh.aspx[^].

The general patten is simple. First, you need to have some event argument class. Use one of available classes or derive your own one from System.EventArgs class, which is only needed when you have to pass your custom parameters to a handler of an event.



class MyEventArgs : System.EventArgs {/* ... */}




Class MyEventArgs
	Inherits System.EventArgs
	' ... 
End Class





然后,声明控制类:



Then, declare the control class:




public class MyControl : Control {

   public event System.EventHandler<MyEventArgs> MyEvent;
   // no initialization ever needed, it cannot be initialized
   public event System.EventHandler MySimlestEvent;

   // and here is the way to invoke events:

   void InvokeMyEvent() {
      if (MyEvent != null) 
          MyEvent.Invoke(this, new MyEventArgs(/* this is how arguments are passed to the handler */));
   }

   void InvokeMySimplestEvent() {
   if (MySimplestEvent != null)
       MySimplestEvent.Invoke(this, new System.EventArgs()); // no arguments
   }

   // in case of user control, use the above two methods in the pre-set event handlers of children

}




Public Class MyControl
	Inherits Control

	Public Event MyEvent As System.EventHandler(Of MyEventArgs)
	' no initialization ever needed, it cannot be initialized
	Public Event MySimlestEvent As System.EventHandler

	' and here is the way to invoke events:

	Private Sub InvokeMyEvent()
		If MyEvent IsNot Nothing Then
				' this is how arguments are passed to the handler 
			MyEvent.Invoke(Me, New MyEventArgs())
		End If
	End Sub

	Private Sub InvokeMySimplestEvent()
		If MySimplestEvent IsNot Nothing Then
			MySimplestEvent.Invoke(Me, New System.EventArgs())
		End If
		' no arguments
	End Sub

	' in case of user control, use the above two methods in the pre-set event handlers of children

End Class



无论是控制类还是不,它也可以是一个结构。这是普遍推荐的模式。您可以使用一些没有sender和eventArgs的自定义模式;它会起作用,但FxCop不会批准它( http://en.wikipedia.org/wiki/Fxcop [ ^ ])。



-SA


这篇关于UserControl自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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