can a menu on a base form be inherited and shown on another form?

  • Thread starter Thread starter Joanne
  • Start date Start date
If a second form inherits the first form this way:
public class Form2 : Form1
I understand that all controls of the first form appear on second form.
Try it! It's really simple!

I've tried with a MainMenu and the same Manu appears in the second form
just inheriting the form, without doing nothing else!

The events of the controls belonging to the first form will also fire
in the inherited form.

Hope it helps.
 
I tried this in VS2005 beta2 CF2.0 projects and the menu doesnt appear on
the second form. the code is below.

I created both forms
I changed form2 to inherit from form1 as marked below
I changed program.cs to run Form2 instead of Form1 as marked below

Do you see the problem?

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

namespace menuinherittest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main()
{
Application.Run(new Form2()); // <== Here
}
}
}

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

namespace menuinherittest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}

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

namespace menuinherittest
{
public partial class Form2 : Form1 //<=== Here
{
public Form2()
{
InitializeComponent();
}
}
}
 
First of all, I'm developing with CF 1.0 and VS.NET 2003.

Anyway, I understand that in order Form2 inherits Form1 correctly,
Form1 must be also run. That is, in my example I run Form1 and in a
button of this form, I opened Form2, which inherited Form1's controls
and appearance.

Maybe (correct me if I'm wrong), if you want to directly show Form2,
you have to run Form1, hide it and then show Form2.

Tell me your experience if you achieve it this way, but as I've told
you, I'm not running VS.NET 2005 Beta 2. Could be a bug?

Regards.
 
I did it with:

Form2 f = new Form2();
f.ShowDialog();

The worst thing is that as Form2 inherits Form1, Form2 does also
contain button1 and its corresponding button1_Click event, so all the
time you press button1, you are opening Form2. ;-)

Have you tried running Form1 and in the Form1 constructor or after
InitializeComponent, do the following:

Form2 f2 = new Form2(this);
**Include an argument referencing Form1 (this) so that you can close
Form1 when *application is exited from Form2. This way does not remain
in RAM memory.
f2.Show();
Form1.Hide();

Hope it helps.
 
Back
Top