Click here to Skip to main content
14,668,030 members
Rate this:
Please Sign up or sign in to vote.
I am having an unexpected problem with visual inheritance in Visual Studio C#. There is a base form, from which several derived forms inherit. Everything compiles nicely, without errors, but the derived forms look exactly like the base form, even though they shouldn't (the derived forms have some controls that the base form doesn't; those controls were added at design time, not at run time, in case this helps; somehow those controls don't appear on the derived forms at run time). Any classes in the project that don't have forms also show the same base form when compiled, even though they don't inherit from it.

Any idea what could be causing this? I've looked over all my code multiple times and still don't see what's wrong with it.

Thank you for the information.
Posted
Updated 1-May-11 5:46am
v2
Comments
Ryan Zahra 29-Apr-11 6:53am
   
Can we see the code for the derived form where you are adding the controls?
Groulien 29-Apr-11 7:03am
   
The Program.cs can't really be the problem unless you've put all the forms code (which I doubt).
By default, the Program.cs only opens a form, not code them.

What you coud do is add the following code to your OnLoad event and check if they are added to the controls collection:
<pre lang="cs">
private void OnLoad(object sender, EventArgs e)
{
foreach(Control C in this.Controls)
{
MessageBox.Show(C.Name);
}
}
</pre>
Olivier Levrey 29-Apr-11 7:20am
   
Do they look the same at design time only or at run time as well?
Member 7883205 29-Apr-11 12:48pm
   
The derived forms look the way they should look in design time. At run time, however, they all look like the form from which they inherited.
Member 7883205 30-Apr-11 7:11am
   
Here is the code for the smallest and simplest of the derived forms (which has no controls except a few labels):


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace AssortedForms
{
public partial class AboutForm : AssortedForms.BaseForm
{
public AboutForm()
{
InitializeComponent();
}
}
}

Rate this:
Please Sign up or sign in to vote.

Solution 1

Odd.
I just tried it (deriving directly from a form is not something I do that often, so I wanted to be sure)

I have an existing form called frmPurchaseType
I added a form called frmDeleteMe
I changed the frmDeleteMe.cs file to:
public partial class frmDeleteMe : frmPurchaseType

I opened the frmDeleteMe in the designer, enlarged it, and added a new button. All the frmPurchaseType controls were present.
I added code to my main form load event:
frmDeleteMe f = new frmDeleteMe();
f.Show();

I ran the app. The form that showed was frmDeleteMe, with the extra button, and all frmPurchaseType controls.

What did you do that I didn't?
   
Comments
yesotaso 29-Apr-11 7:53am
   
Indeed odd. I first thought about access modifier for InitializeComponent, but then realised the private modifier ensures initializer of both base and derived form runs...
Sergey Alexandrovich Kryukov 29-Apr-11 11:38am
   
Of course. What you do should work without a problem, my 5.
What OP observes is just... I posted my answer. :-)
--SA
Rate this:
Please Sign up or sign in to vote.

Solution 2

Form inheritance works without any problem. What you see is just a bug, nothing else. Without a look at you project it's hard to say what's wrong.

A note: there is no such thing as "parent form" in relation to other form. [EDIT] Well, quite formally, it does exist as Form is Control; but if you try to make something a parent of a Form instance, an exception is thrown unless you assign a Form.TopLevel to true; in this case you can make a form a child, but why? The results of it are ugly. [END EDIT] As a Control is a parent to form children, which are controls. A form can be the "Owner form". What you tried to say is "base class".

—SA
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100