I am trying to use form inheritance in C#/NET 2.0 and find that some things (which were possible in other systems) do not seem to work here. Actually, even inheriting a form seems do be a bit of a chore (and the C# Help does not mention it at all).

In particular, some controls, if put on the parent form, cannot be managed in the child form. A good (and really obnoxious) example is DataGridView. If I put a grid on the parent form, I can do nothing with in in the child form - all properties are gray and I cannot add columns and so forth. Changing the Modifiers property of the grid in the base class does not help (although I have also noted that some controls other controls do not show up on the child form until they are at least elevated from private to protected).

I have a situation with a group of lookup tables which are very similar except for a few non-essential columns and other details which vary from table to table. Because of the above problem I must not only repeat design operations in each form, I must repeat event code in each one because I cannot add the grid until the child class is defined. This is very inefficent and it is not the only example.

Is there any way to get form inheritance to work like it does in other languages or is it just not supported very will in C#?

Recommended Answers

All 5 Replies

Agreed, it is a little trickier in C# than some languages.

Would placing this datagrid in a user control and adding it to the forms work for you?

Agreed, it is a little trickier in C# than some languages.

Would placing this datagrid in a user control and adding it to the forms work for you?

Well, it is worth a try - I'll report back.

Well, it is worth a try - I'll report back.

I checked this out and using a User Control just complicates the issue further.

I guess form inheritance is just one of those areas Microsoft didn't pay much attention to. Considering that they spent years trying to convince people that VB6 was OOP, I guess I shouldn't be surprised. I'll just copy and paste a lot of code instead of doing something more civilized.

Thanks for the suggestion anyway.

Yeah its a big mission, but unfortunately its possible....

your normal program.cs looks like :

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace GetMousePosition
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

now this is what you do :

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace GetMousePosition
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1(Form2));
        }
    }
}

its something along those lines

have fun

just go to add new item and select inherited form... it will ask from which form it should inherit..u should build the solution before doing this.. otherwise it will not show the base form

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.