Visual C++.NET Control Class Libraries
- Oct 25, 2002
See all Sams Teach Yourself on InformIT Programming Tutorials.
It's been estimated that the market for custom controls and components will be in the billions of dollars in the coming years. Beginning with the emergence of COM and ActiveX, companies are realizing the full potential of code reuse through component-based architectures. Through the use of custom controls and components, you can save precious development time and money that is better spent on the more important aspects of your project. Just as Henry Ford revolutionized the manufacturing process with the application of assembly lines, component-based architectures have done the same for software development.
Creating custom controls with the previous version of Visual Studio meant using COM and, more specifically, ActiveX controls. Visual Studio .NET has since changed this method to better align with the design goals of the .NET Framework. Although creating ActiveX controls is still possible and necessary in some instances, .NET controls free you from learning the intricacies of COM interfaces and automation compatibility as well as other difficult issues that arise from using ActiveX controls.
In this hour you will learn:
How to create a custom control with Visual C++ .NET
How to utilize that control within a managed application using Windows Forms
How to place your control onto the Visual Studio .NET Toolbox
How to create control properties and integrate them into the Property Browser window within the IDE
Controls Within the .NET Framework
Even though the .NET Framework contains many controls you can use when creating a project that uses Windows Forms, there may be instances when you need a custom control that either expands on the functionality of an existing control or adds functionality that can't be found with other controls. In this hour, you will be creating a custom control that can then be used with any of the .NET languages.
Creating a custom control is similar to creating any other project that uses Windows Forms. However, rather than creating an executable that you can run to test your application, you'll create a control that's contained within a dynamic link library (DLL). Of course, by creating a DLL you are at a disadvantage because you must also create a separate project that uses that control. However, creating a test harness to test your control isn't any different from creating the Windows Forms applications you've already created within this book. Adding your custom control to a Windows Form isn't any different from adding any of the controls (buttons, labels, and text boxes, for example) that ship with Visual Studio .NET.
When creating a new control, you have several options available to you based on what type of design you prefer. These options are all based on what base class your control inherits its base functionality from. You have three main options to choose from:
Inheriting from an existing Windows Form control
Inheriting from the UserControl class
Inheriting from the Control class
Inheriting from an Existing Windows Form Control
The easiest option you have is to inherit from an already established control. You would want to do this if there is a control that provides similar functionality to the control you wish to create. Inherit from an existing control if you simply want to add new events or properties while still retaining a majority of the basic functionality of the base control.
Inheriting from the UserControl Class
A user control is a control that allows you to combine several controls within one single composite control. In other words, a user control contains one or more Windows Form controls combined together into a single entity. An example of a control of this type could be a database-navigation control. You could have several text boxes or labels contained within a single user control as well as buttons that change the values of those controls as the user navigates through the database.
Inheriting from the Control Class
The Control class provides the basic functionality needed to create a custom control. By inheriting from this class, it is your responsibility to perform any custom painting by overriding or adding a handler for the Paint event. You would want to use this control if no other control contains similar functionality. For this hour's lesson, you will be creating a control based on this design.