9.2 Creating Composite Controls

 <  Day Day Up  >  

You want to create a control that is a combination of several controls that interact with each other to implement certain functionality.


Technique

To create a composite control, choose Project, Add User Control (Alt+P, U). When the Visual Studio .NET Integrated Development Environment (IDE) creates the control, you can use the forms designer to visually lay out your control, as shown in Figure 9.2. You can set properties and create event handlers using the same procedures as in a regular Windows Form application. Furthermore, you can create additional custom properties and methods to customize the behavior of the composite control.

Figure 9.2. A composite control within the Forms Designer and the result of running it within an application.

graphics/09fig02.gif

Comments

Creating composite controls is similar to creating a regular Windows Form application. When the control code is generated by Visual Studio, the forms designer opens, allowing you to visually design the control. Design the control just as you would any other Windows Form by setting properties and creating any necessary event handlers. When you look at the code that is generated, you'll even see that the only noticeable difference between the control and a Windows Form is the base class that your class derives from. In fact, in most cases you can easily convert a Windows Form to a composite control by changing the base class to System.Windows.Forms.UserControl and removing AutoScaleBaseSize , which is not defined within the UserControl class.

Composite controls are best when you need to encapsulate functionality within a single module that you can then use within several forms and applications. In Figure 9.2, you can see a composite control used to perform some sort of application login. Although you could leverage the code using classic copy and paste methods for each Windows Form that uses it, the ramifications of doing so are quite apparent because a change in the original code is multiplied by the projects that use that code. In fact, you might not identify all the places within the forms that you create across multiple projects as prime candidates for a composite control. For instance, if the applications that you create use a company logo, create a composite control that contains a PictureBox control that displays the company logo. You can then change every logo in all applications that use that composite control simply by changing the control's source code rather than individually edit each project's source code.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net