Using Visual Inheritance


Visual inheritance is a tool that allows you to create a form that inherits from a parent form. The "visual" part of the inheritance refers to the fact that all of the control layout and property information from the parent form is provided to the child form. For example, if you wanted to create a single parent form that contained the company's logo and the company name on it that could then be inherited by all forms used by all applications within the company, visual inheritance would do the trick.

To see how this works, create a new Windows Forms application and rename the default Form1 form to parentForm. VS 2005 will prompt you to refactor that name throughout the solution; click OK to do this. Next, add a Panel to the form with a white background and set its Dock property to Top. You can then drag a PictureBox and a Label into this top area to create your fictitious company name and logo.

To create a form that visually inherits from the parent form, right-click the project and choose Add, Windows Form, Inherited Form. Call the new form ChildForm. Within the designer it will look very much like the parent form, except that on the child form you cannot modify the properties of any of the controls on the parent form. Figure 36.6 shows the parent form and child form in action.

Figure 36.6. Visual inheritance.


Visual inheritance is just standard .NET inheritance with full design-time support. As such, you can add a property on the parent form that can be modified by the child form. For example, you can create a Slogan property on the parent form that will change the text of the company's slogan:

public string Slogan {     get { return label2.Text; }     set { label2.Text = value; } } 


And you can modify that slogan on the child form simply by changing the property value, as shown here:

public ChildForm() {     InitializeComponent();     Slogan = "Our company is your company."; } 


Combining visual inheritance with other advanced GUI techniques such as using GDI+ as well as the power of the new data-binding features in Windows Forms 2.0 can create some amazing interfaces that were nearly impossible to create using previous versions of Windows Forms.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

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