September 5 2010




 
Search Blog Entries:



What is this?

Article Details
 
Visual Form Inheritance with the .NET Compact Framework

C# Preprocessor – Help is on its way

Thanks to the fact that we can use the C# preprocessor for conditional compilation there is a possibility to have a limited form of designer support when inheriting a form from another form. If you just try to inherit a form and switch to designer view, the designer will give an error message that the specified cast is not valid. The reason is that the designer expects Forms to be derived from System.Windows.Forms.Form. If we could achieve a way to satisfy the designer by deriving from System.Windows.Forms.Form, but derive from another form during compile time we can use the designer to add additional controls to a form. There is one caveat, we are not able to see the controls on the form that we inherit from the parent form, but apart from that we can get a complete designer experience in the inherited form. Thanks to conditional compilation we can derive from System.Windows.Forms.Form during design time and from another form during compile time. To achieve that we define a symbol at the top of the source file that contains our derived form. We than use conditional compilation to derive from the proper form.

#define DESIGNER_SUPPORT

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace
FormInheritance
{
   // Sample form that derives form another form. To
   //
be able to use the designer to add controls to
   // our newly created form we derive from
   // System.Windows.Forms.Form during design time
   // by defining 
defining DESIGNER_SUPPORT. To
   // compile the application make sure to 
have 
   // DESIGNER_SUPPORT not defined (by commenting out
   // the #define on 
top of this source file).

   public class Form2 :
#if DESIGNER_SUPPORT
          System.Windows.Forms.Form
#else
          Form1
#endif

Listing 1 - Deriving from a base class using conditional compilation


Using the designer to create the base form

The base form is nothing special. We can just use the designer to create a form. However, since we are creating a form that will be used for inheritance purposes make sure to add only controls to the form that will be shared over the entire family of forms that will inherit from this base form. As an example we create a very simple form containing a label and a button.

The base class in VS.NET 

Figure 1 - Our base form class in the designer

In this example we target a PocketPC device. Since one of the controls that we inherit in other forms is a button we now need to make sure that the button will invoke an event handler that corresponds with the particular form when the button is clicked. To achieve that we need to modify the source code for the Form slightly. Using the designer to add a button to the form, a private member variable of type Button will be added to our form class:

private System.Windows.Forms.Button button1;

Furthermore, when we use the designer to add a button_Click event handler, it will create a method with the following signature:

private void button1_Click(object sender, System.EventArgs e)

Because of the fact that we want to add specific behavior to the button in derived forms, it can not be private to this particular form. Instead we want to make the button protected, so it can be reached from within a derived form. We also need to modify the button_Click handler to be able to override it with a specific event handler for each of the derived forms. So after creating the base form we locate the declaration of each of the controls that has a need for specific behavior in derived forms and change the access modifier from private to protected. The next thing to do is locate the event handlers for those controls and make them protected and virtual. In the given example the declaration of button1 will look like:

protected System.Windows.Forms.Button button1;

The declaration of the event handler will now look like:

protected virtual void button1_Click(object sender, System.EventArgs e)

Note that you have to redo this everytime a control is removed and added again using the designer.

Using the designer to create the inherited form

Creating the inherited form in the designer simply starts by adding a new form to the project. In the designer we get a new blank form which we will use to inherit from the previously created form. Unfortunately it is not possible to make the controls that are available on the base form visible on the derived form as well. However, we can simply add new controls to the form using the designer.

The derived form in VS.NET 

Figure 2 - This form will inherit from the parent form

To make sure that we derive from the previously created base form we need to modify the source code that has been generated while designing the form according to listing 1. Another thing that we should be aware of is the fact that our parent form has a button defined. Since we inherit all functionality of the base class it means that the derived form contains a button as well. Without adding a specific button_Click method for the derived form, the event handler of the parent form will be invoked. In the case of this sample application that would definitely lead to problems, since we use the button_Click method of the parent form to display the inherited form. Not overriding the button_Click method for the inherited form would lead to displaying the inherited form over and over again. To add a new button_Click method specific for the derived form we simply declare a new button_Click handler in the derived form that overrides the one in the parent form. The declaration of this event handler looks like:

protected override void button1_Click(object sender, System.EventArgs e)

To make sure that the correct event handler is invoked when the button is clicked we also need to add the event handler to the button. The typical location to do this is in the constructor of the derived form:

public Form2()
{
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();
   this.button1.Click +=
       new System.EventHandler(this.button1_Click);
}

To keep the sample code as easy as possible, the button_Click handler in Form2 simply sets the DialogResult to OK and closes the form again. Even though we used C# for this article, the same technique is also usable with VB.NET.

Inherited form shown in the PPC 2002 emulator 

Figure 3 - The inherited form in action.

Conclusion

That is all there is to come as close to visual inheritance of forms as you can with the .NET Compact Framework and Visual Studio.NET 2003. The fact that we can’t see the inherited controls on the derived form makes this approach not ideal. However, using some of the features of the C# preprocessor it is possible to visually design forms derived from other forms.

 

 

 

 
Back








SpiralFX Technology Solutions
www.spiralfx.com


Do you want to learn developing a full blown Windows Mobile Application? This article and accompanying multimedia content will help you to do so. It will be extended over the upcoming weeks / months, so check back regularly.
 
Read Full Article