Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I've noticed every event in .Net has something akin to "Sender as Object" as an event argument.

 

I'm making use of my own custom events in controls and wondering if there is a reason to follow this convention?

 

if I'm creating an event on a control that is raised whenever one of 12 panels backcolor is changed by a user (via color dialogue), an event is raised to tell the host that the user has made a change.

 

I'm guessing that many of these events have been inherited back and forth which is why they use object.

 

I'm not even sure how to enable "e as System.EventArgs" other than just passing it through the event which raised my event (Panel_Click event).

  • Administrators
Posted

If you want your control to behave like the standard controls (i.e allow multiple events to be routed to a single event handler) then you should really follow this model.

 

In practice it is easy enough to do - if you require no extra information to be passed to the event handler then just use System.EventHandler as an event type

   Public somethingHappened As System.EventHandler

   Protected Overridable Sub OnSomething()
       somethingHappened(Me, New System.EventArgs)
   End Sub

putting the raising of the event in it's own function (On.......) makes it easier to raise from multiple places and cleaner for code that inherits your control.

 

If you need to pass custom information then create a class that derives from System.EventArgs and add any relevant properties required. Then create your own delegate for the event.

Public Class MyEventArgs
   Inherits EventArgs

   Private _OldColour, _NewColour As Color

   Sub New(ByVal oldColour As Color, ByVal newColour As Color)
       _OldColour = oldColour
       _NewColour = newColour
   End Sub
   Public ReadOnly Property NewColour() As Color
       Get
           Return _NewColour
       End Get
   End Property
   Public ReadOnly Property OldColour() As Color
       Get
           Return _OldColour
       End Get
   End Property
End Class

'to use the above class
   Public Delegate Sub ColourChangedEvent(ByVal sender As Object, ByVal e As MyEventArgs)

   Public ColourChanged As ColourChangedEvent


   Protected Overridable Sub OnColourChanged()
       ColourChanged(Me, new MyEventArgs(color.Red, color.Black)
   End Sub

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

  • *Gurus*
Posted

Just to add to what PlausiblyDamp said, based on my own experience with writing custom controls and components.

 

Your OnEventName protected method should accept one parameter, an instance of the EventArgs which will then be passed on to the event. Also, the definition of your delegate (for custom event signatures) should be namespace-scoped instead of class-scoped. If you scope it to a class, some Visual Studio designers will have problems serializing code for it.

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • Administrators
Posted

Whoops - forgot that bit, what happens when you are trying to code and hold a conversation at the same time.

 

Denaes - go with Divil's suggestion which would look similar to

Protected Overridable Sub OnColourChanged(args as MyEventArgs)

       ColourChanged(Me, args)

   End Sub

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...