Visual C# | Inheritance exercise | Create a form template by inheriting from Form class

This exercise is provided to allow potential course delegates to choose the correct Wise Owl Microsoft training course, and may not be reproduced in whole or in part in any format without the prior written consent of Wise Owl.

Software ==> Visual C#  (79 exercises)
Version ==> Any version of C#
Topic ==> Inheritance  (5 exercises)
Level ==> Relatively easy
Subject ==> C# training

You need a minimum screen resolution of about 700 pixels width to see our exercises. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Suppose that whenever you create a form, you always set values for certain properties as follows:

Property Value
StartPosition System.Windows.Forms.FormStartPosition.CenterScreen
FormBorderStyle FormBorderStyle.FixedDialog
Text You set the caption of the form when you first create it.
Height 120, 300 or 500, depending on whether the form is tidgy, mediumish or huge.
Width 200, 400 or 700, depending on whether the form is tidgy, mediumish or huge.

What you'd like to be able to do is to write code like the following:

// create a new template form, setting its caption

FormTemplate ft = new FormTemplate("Wise Owl");

ft.size = FormTemplate.FormSize.Tidgy;


When you attach this code to a form button and run it, you'd like it to create and show a form on screen:

New form created in code

Clicking on a button in a test form should run this code to display a new form, with the caption, properties and size requested.


To get started, create a new class called FormTemplate, which should include an enumeration to determine the form size:

public enum FormSize






Your class will need to use the System.Windows.Forms namespace, and should inherit the Form class (so that it will behave like a form, with knobs on).

Add a constructor to your form which takes the form caption as an argument, and within this constructor set the start position, border style and caption.

Add a write-only property called Size which sets the dimensions of the form according to the value of the FormSize enumeration set.

Create the code shown at the start of this exercise to test out your class; running it should bring a form up on screen of the required size and with the required behaviour! 

You can unzip this file to see the answers to this exercise, although please remember this is for your personal use only.
This page has 0 threads Add post