Control Base Classes

function OpenWin(url, w, h) { if(!w) w = 400; if(!h) h = 300; window.open(url, "_new", "width=" + w + ",height=" + h + ",menubar=no,toobar=no,scrollbars=yes", true); }

The .NET base Class Library provides base classes that provide various levels of support for building controls. The .NET base Class Library is at the heart of the .NET platform. You can think of the .NET base Class Library as similar to the C runtime or VB runtime on steroids. In addition to providing common types such as Int32 and string, the base Class Library provides for database access, collections, windows forms, and Web forms development, to name a few.

Each derived control base class extends the functionality provided by the base class from which it inherits. Figure 2.2 shows the control classes provided by the base Class Library.

Figure 2.2. Control base classes.

figure 2.2. control base classes.

The control hierarchy shown in Figure 2.2 should be used to determine where to begin when a new custom Windows Forms control is being built. Each derived class extends and in some cases modifies the behavior of its parent class. Every common control available with VS .NET derives from one of the classes in the control hierarchy.

Component

At the very root of all control base classes is the Component class or the fully qualified name System.ComponentModel.Component. The Component base class serves as the base class for all classes within the System.Windows.Forms namespace. Any class that inherits from the Component base class basically states that the class will free up any resources it uses through the invocation of its Dispose method. In essence, any class that inherits from the Component base class can be told when to clean up without waiting for the object instance to be collected by the .NET Garbage Collector.

Control

The Control class represents the single most significant base class in that it provides all the necessary plumbing for control development. The Control base class provides for message routing, both keyboard and mouse, security, size and position, and the creation of the underlying window handle or hWnd, for instance. Although the Control base class does not provide any default painting, it does provide for all the basic services of a .NET control, including the implementation of ActiveX control interfaces. By providing all the ActiveX control interfaces, custom controls developed in .NET can be hosted within Internet Explorer and other ActiveX control hosts.

According to the documentation, classes do not typically inherit from the Control base class directly, but rather from the UserControl class. The developers for the Windows Forms controls apparently didn't read that documentation, because the controls found in the Windows Forms namespace typically inherit directly from the Control base class. The System.Windows.Forms.Control class serves as the base class for the DataGrid, DateTimePicker, Label, GroupBox, ListControl, and so on. Actually, the intention is that most developers will be developing simple UserControl derived controls and do not require the low-level control afforded by twiddling with the underpinnings of the control framework.

ScrollableControl

As the name suggests, the ScrollableControl base class provides the capability for a control to scroll its contents. Setting the AutoScroll property to true provides the scrolling. The Panel control inherits from this control base class.

ContainerControl

The ContainerControl base class provides the necessary wiring for hosting other controls such as buttons, labels, and the like, and it serves as the base class for the UserControl, the PropertyGrid, and the Form class. The main benefit derived from using the ContainerControl is focus management and mnemonic handling for child controls contained within the ContainerControl. Focus management deals with handling the Tab key and setting focus to the next control based on the tab order of the child controls. Mnemonic handling is the processing of shortcut or accelerator keys to set focus to the corresponding control.

Using the ContainerControl object provides processing of events such as the Tab key and focus information about child controls. It is important to note that a ContainerControl will not receive focus but rather focus will be set to the first child control of the container.

UserControl

The .NET UserControl is very much the same as the User control concept found in earlier versions of Visual Basic. Essentially, a UserControl is a simple method for creating a composite control consisting of one or more controls. Because the UserControl class derives from the ContainerControl base class, it inherits all the focus management and control management implemented by the ContainerControl. Control management entails hosting child controls and managing the events of the child controls. In essence, a UserControl is a fully self-contained control that also generally includes some amount of business logic such as data entry validation and even database access if necessary.

The UserControl base class provides a solid foundation for creating reusable controls consisting of presentation and data validation to be reused in an application. Consider building an application in which it is necessary to obtain a customer's address, suppliers' addresses, and other various addresses. Each of these addresses will be subject to similar data validation, such as verifying the zip code for a city and the city within a state.

When a UserControl is created to handle this common task, the UserControl can then be used anywhere within the application that requires validating the user-entered address.

Address UserControl

To gain an understanding of basic control development, designing and creating a simple UserControl provides an easy starting point. Taking the address validation scenario presented previously, designing and implementing a basic UserControl to fulfill this purpose requires only the following few simple steps:

  1. Create a new C# Windows application.

  2. Add a new UserControl to the project.

  3. Design the UserControl user interface (see Figure 2.3).

    Figure 2.3. The user interface for the AddressControl UserControl.

    figure 2.3. the user interface for the addresscontrol usercontrol.

  4. Compile the project.

  5. Select the new UserControl from the Toolbox and drag it onto the main form (see Figure 2.4).

    Figure 2.4. The AddressControl placed on Form1.

    figure 2.4. the addresscontrol placed on form1.

Beginning with step 1, create a new Windows application project with the name "Address UserControl." This project will serve to contain both the custom UserControl and the Windows Form that will be used to host the AddressControl. After the project is created, it's time to add a new UserControl to the project. You can do this by selecting Add New Item from the File menu or by right-clicking on the project in the Solution explorer and selecting New UserControl from the Add menu item. Name the new UserControl AddressControl.

If the AddressControl is not currently in design mode, double-click the AddressControl.cs file in the Solution explorer to bring up the UserControl in design mode. Next, construct the AddressControl UI so that it matches the control shown in Figure 2.3.

After the UI for the control has been constructed, double-click the Validate button to bring up the code window for the control. Listing 2.1 provides the implementation for the click event handler and a small helper method for the control.

Listing 2.1 Logic for the AddressControl Validate Button
  1: private void btnValidate_Click(object sender, System.EventArgs e) {  2:     //Ensure there are no empty text boxes  3:     if( !ValidateTextBox( txtAddressLine1 ) ) {  4:         txtAddressLine1.Focus( );  5:         return;  6:     }  7:     if( !ValidateTextBox( txtAddressLine2 ) ) {  8:         txtAddressLine2.Focus( );  9:         return; 10:     } 11:     if( !ValidateTextBox( txtCity ) ) { 12:         txtCity.Focus( ); 13:         return; 14:     } 15:     if( !ValidateTextBox( txtState ) ) { 16:         txtState.Focus( ); 17:         return; 18:     } 19:     if( !ValidateTextBox( txtZip ) ) { 20:         txtZip.Focus( ); 21:         return; 22:     } 23: } 24: 25: private bool ValidateTextBox( TextBox textBox ) { 26:     if( textBox.Text.Length == 0 ) { 27:         MessageBox.Show( this, string.Format( "{ 0} is empty", textBox.Name ) ); 28:         return false; 29:     } 30:    return true; 31: } 

The code for the AddressControl is fairly simple in all respects. Each TextBox on the control is tested to ensure that the user in fact entered something.

With the code in place, the next step is to build the project. The project must be compiled before the new AddressControl can be used and placed on the main form. Attempting to use the AddressControl without building the project results in a rather cryptic message from the IDE about not being able to create the control.

After the project has been compiled, switch back to the main form, which should be Form1.cs. Now available on the bottom of the Toolbox in the Windows Forms tab is the newly created AddressControl. As with any other control within the Toolbox, this new control can be dragged onto the form and sized appropriately (see Figure 2.4).

Custom UserControl style controls are handy when building applications in which the UI and business logic are seemingly tied together, such as in the case of data validation. Also, because UserControls are by far the simplest style of control to construct, they allow for implementing quick and simple solutions rather than constructing complex controls from scratch.



    .NET Windows Forms Custom Controls
    User Interfaces in VB .NET: Windows Forms and Custom Controls
    ISBN: 1590590449
    EAN: 2147483647
    Year: 2002
    Pages: 74

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