<a href="http://www.micropoll.com/akira/mpview/585320-168921">Click Here for Poll</a><a href="http://www.questionpro.com" title="online surveys">Online Survey</a><BR> | <a href="http://www.micropoll.com" title="Website Polls">Website Polls</a><BR> | <BR><a href="http://www.micropoll.com/akira/MicroPoll?mode=html&id=168921">View MicroPoll</A></div>

Visual Studio 2010!

Read now >

Windows Mobile Development Thoughts

Read now >

View Now
DevSource RSS FEEDS
XML Want an easy way to keep up with breaking tech news? And the Get DevSource headlines delivered to your desktop with RSS.
ADVERTISEMENT
ADVERTISEMENT

 

DevSource.com: Your Source for Visual Studio on Facebook
ADVERTISEMENT
Subclassing Windows Forms Controls
By Peter Aitken

Rate This Article: Add This Article To:

Subclassing Windows Forms Controls - ' Custom Methods '
( Page 4 of 5 )

Custom Event Handlers

Creating custom methods follows the same general approach as creating custom properties. You can create totally new methods, or you can override existing methods. Base class methods can be accessed using the MyBase keyword, as needed. Note, however, that many methods of existing control classes are not overridable. If you try to override such a method, the VB.Net editor will inform you. Also, you can look in the online documentation to see which of your base control's methods are overridable.

Custom Event Handlers

Creating a custom event handler in your derived class follows a somewhat different approach than creating custom properties and methods. Rather than directly overriding the event handler in the base class. Rather, you make use of a set of overridable methods that are named using the format OnXXXX() where XXXX is the name of an event.

For example, the TextBox control has OnClick(), OnDoubleClick(), and OnMouseMove() methods, among many others. Each of these methods is connected to the underlying event in both directions. This means:

  • If the event occurs, the method is called (in addition to the regular event procedure).
  • If the method is called, the event is triggered.

For the present purposes, the importance of the OnXXXX() methods is that you can use them to indirectly override the base class events. For example, the following code in your custom control overrides the base control's Click() event, displaying a message box when the custom control is clicked:

Protected Overrides Sub OnClick(ByVal e As EventArgs)
    MyBase.OnClick(e)
    MsgBox("Thanks for clicking me!")
End Sub

This example illustrates two things. First, each OnXXXX() method has a signature — in other words, the type of its argument(s). The method you create to override the base class method must have the correct signature. Second, your method must call the corresponding method in the base class, using the MyBase keyword. This is necessary to ensure that the event is processed in the normal manner.

Here's an example of overriding the OnMouseDown() and OnMouseUp() methods. The result is that the custom control's foreground color changes color: to red when the mouse button is depressed, and to black when the button is released.

Protected Overrides Sub OnMouseDown(ByVal e As MouseEventArgs)
    MyBase.ForeColor = Color.Red
    MyBase.OnMouseDown(e)
End Sub

Protected Overrides Sub OnMouseUp(ByVal e As MouseEventArgs)
    MyBase.ForeColor = Color.Black
    MyBase.OnMouseDown(e)
End Sub


 
 
>>> More Using Microsoft Visual Studio Articles          >>> More By Peter Aitken