C# How to Subclass a Windows Form


Author
Message
Ger Cannoll
Ger Cannoll
Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)
Group: StrataFrame Users
Posts: 430, Visits: 507
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
Aaron Young
Aaron Young
StrataFrame User (421 reputation)StrataFrame User (421 reputation)StrataFrame User (421 reputation)StrataFrame User (421 reputation)StrataFrame User (421 reputation)StrataFrame User (421 reputation)StrataFrame User (421 reputation)StrataFrame User (421 reputation)StrataFrame User (421 reputation)
Group: StrataFrame Users
Posts: 277, Visits: 1.1K
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.
Ger Cannoll
Ger Cannoll
Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)Advanced StrataFrame User (628 reputation)
Group: StrataFrame Users
Posts: 430, Visits: 507
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.
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search