StrataFrame Forum

C# How to Subclass a Windows Form

http://forum.strataframe.net/Topic31091.aspx

By Ger Cannoll - 4/2/2012

This follows on from another thread with some other issues, but I am trying to subclass a Form with C# and say change the BackGround Colour to Blue.(Simple example but if I can get this to work, I should be able to get any other property to work)

I would have thought that this should be fairly simple, but cannot get it to work. I have subclasseed other controls that are dropped on a form ok

Has anybody subclassed a Form using C#. and changed a few properties ?

A sample of Code to do this in C# would really be appreciated. I have searchhed the forums here and Google, but still cant get it to work
By Aaron Young - 4/3/2012

The following class is a sub-class of the SF form and it changes the back colour to red.

using System;
using System.Reflection;

namespace MyApplication.Forms
{
    public partial class BaseForm : MicroFour.StrataFrame.UI.Windows.Forms.StandardForm
    {
        public BaseForm()
        {
            InitializeComponent();

            BackColor = System.Drawing.Color.Red;
        }
}

This form is sub-classed from the above BaseForm and would have a red background by default

using System;

namespace MyApplication.Forms
{
    public partial class myForm1: BaseForm
    {
        public myForm1()
        {
            InitializeComponent();
        }
}

This form is sub-classed from BaseForm but the default red colour is changed to blue for this form only.

using System;

namespace MyApplication.Forms
{
    public partial class myForm2: BaseForm
    {
        public myForm2()
        {
            InitializeComponent();

            // Override the base class red color
            BackColor = System.Drawing.Color.Blue;
        }
}

Hope it helps.
By Ger Cannoll - 4/3/2012

Hi Aaron.

Many thanks for that, and it worked perfectly.

I think I was missing the constructor bit, but when I copied your  code  verbatim, it worked fine.