Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed



email article
printer friendly
get the code
more resources

Tap the Power of Inherited WinForms
Mix inheritance and Windows Forms to solve recurring UI-related problems.
by Francesco Balena

March 4, 2005

Technology Toolbox: VB.NET, C#

Everything is an object in the marvelous world of managed code, from simple entities, such as numbers and strings, to complex UI objects, such as controls and forms. As a matter of fact, a form is simply a class that inherits from a .NET type named System.Windows.Forms.Form. Thanks to inheritance, your form knows automatically how to behave when a user drags its borders to resize it or clicks on the upper-right button to close it. In addition, Visual Studio .NET knows how to display the form at design time, so that you can drop controls on its surface and have VS.NET generate the code that instantiates control objects and set their properties accordingly.

ADVERTISEMENT

Forms don’t need to inherit directly from the System.Windows.Forms.Form type. Instead, they might inherit from another class you’ve defined in the same executable or in a separate DLL, provided the other class inherits from the Form type, either directly or indirectly. In many cases, inheriting from an intermediate class can offer plenty of advantages, as I’ll demonstrate in this month’s column.

Use a Base Form for Your Dialog Boxes
Consider the simple task of creating a dialog box with the typical OK and Cancel buttons (see Figure 1). Have you ever considered how many actions you need to create a functional dialog box? You must change the form’s FormBorderStyle to FixedDialog, add the two buttons, set their names and captions, possibly anchor them to the right or bottom border (so you don’t have to move them if you need to resize the form later), set the form’s AcceptButton and CancelButton properties, and finally write the handlers for the buttons’ Click events. The entire procedure might take you about one minute, but if you multiply this minute for each and every dialog box you’ve created in recent years, you’ll reckon that’s a lot of wasted time and code.

Thanks to form inheritance (otherwise known as visual inheritance, in VS.NET jargon), you can prepare a DialogFormBase class that will act as the base form for all the dialog boxes you’ll define in the future. You create such a base form as if you were creating a regular form, except you ensure that the Modifiers property of all controls is set to Protected, instead of the default Friend (VB.NET) or Private (C#) scope. If you omit this step, you won’t be able to access these controls and change their properties from inside derived forms.

Next comes the critical part of the entire operation. You can create a useful and reusable base form if you stick to some design principles, even though they force you to write slightly more code than you’d do in a regular form. Specifically, all the events raised by controls in the base form should delegate to a protected and overridable procedure, which in turn performs the default action for that control (see Listing 1). You’ll see the benefits to this verbose approach in a moment (download the companion code samples for this column, and see the sidebar, “Look for More at .Net2TheMax”).

This article requires registration. Please login below or click here to register.
 
E-mail Address:
Password:
Remember me:
 



Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
| | Discussions | Newsletters | FTP Home