Click here to Skip to main content
15,608,671 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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

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!
Sergey Alexandrovich Kryukov 15-May-13 12:30pm    
Not exactly the right MSDN page. OP needs to know declaration of event instances, the class members.
Yuriy Loginov 15-May-13 12:41pm    
fair enough, what I forgot to mention is that when creating a custom control you need to create custom events as well. In this case you want to declare

Public Event SelectedIndexChanged(ByVal sender As Object, ByVal e AS EventArgs)

and then raise the event

RaiseEvent SelectedIndexChanged(this, e)

You most likely will want to raise the event in response to the SelectedIndexChanged of the child DropDown contained in the custom control.
Sergey Alexandrovich Kryukov 15-May-13 12:47pm    
I provided a very detailed answer, please see.

1 solution

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:[^].

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

It does not matter if it is control class or not, it can be a structure, too. This is the universal recommended pattern. You can use some custom pattern without "sender" and "eventArgs"; it will work, but FxCop won't approve it ([^]).

Share this answer
Yuriy Loginov 15-May-13 12:51pm    
5- a few spelling mistakes: FxCop not FxCope and System not Systen :)
Sergey Alexandrovich Kryukov 15-May-13 12:52pm    
Thank you very much.
rusoaica_ 15-May-13 13:00pm    
You are right, i learned how to use them but never got to the point of fully understanding how they work. Perhaps i should take a break and assimilate these knowledges. Thanks for the starting point!
Sergey Alexandrovich Kryukov 15-May-13 13:02pm    
My pleasure.
Are you accepting the answer formally (green button)?
rusoaica_ 15-May-13 13:04pm    
i thinik i did it already... ths is my first question on this site, i don't know how to pick the answer of the question or give reputation or vote, whatever this site uses... if i forgot something, please do tell me

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900