<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 - ' First Steps '
( Page 2 of 5 )

While the .Net Framework provides a rich set of Windows forms controls, there are almost surely times when you need something else. Creating a custom control can take one of two paths: you can create it completely from scratch, or you can subclass an existing control. This article shows you how to use the second technique.

When you subclass an existing control, the new control inherits all of the members of the base control as well as its visual interface (if any). This is only the starting point, of course. You will then to modify the control to provide the custom functionality you want.

First Steps

Custom controls are created as part of a Windows Control Library project, and the first step is to create a new project or open an existing one. Be sure to assign a meaningful name to the project, because this will be the namespace for all of the controls it contains.

If you create a new Windows Control Library project, it open with one new, blank UserControl. If you open an existing project, you add a new UserControl by selecting Add UserControl from the Project menu.

The next task is to change the new control from a UserControl to one that subclasses an existing control. Here are the required steps:

  1. In the Solution Explorer, select the new UserControl and display its code. It will look like this (in this and other listings, the Windows Form Designer code is not shown):
  2. Public Class UserControl1
        Inherits System.Windows.Forms.UserControl
    End Class
  3. Change the name of the class from the default name (e.g., UserControl1) to the name to use for your new control.
  4. Edit the Inherits statement so the class inherits from an existing control rather than from the UserControl class. Your code now looks something like this:
  5. Public Class MyNewControl
        Inherits System.Windows.Forms.TextBox<
    End Class
  6. Be sure the new control is selected in the Solution Explorer, then change its File Name property. You can use any name you like, but I suggest using the same name as you assigned to the class.
  7. Select Save All from the File menu. You'll see that the entry in the Solution Explorer changes to reflect the new filename you assigned.

At this point, you will note that the Designer is no longer available. This is because your control inherits the visual interface of its base class, so there are no modifications you can make in the designer. You can make modifications to the new control's visual appearance in code, but that is beyond the scope of this article.

Your new control could be used at this time, but so far it is nothing more than an exact duplicate of its base class, so there is no point in doing so. The next steps are to customize the control by means of its properties, methods, and events.



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