Reputation: 1391
I have a bunch of mdichild forms already designed and would Like to show the forms as a mdichild. I set the main form to mdi and I am able to show one of the forms as a mdichild correctly. The code that is giving me a hassle is:
public partial class KeyboardSettingsForm : Form
{
private mainForm _mForm;
public KeyboardSettingsForm()
{
InitializeComponent();
_mForm = new mainForm(); //<---mdiparent
this.MdiParent = _mForm; //<---Commenting out this line shows the form
this.Shown += new System.EventHandler(this.KeyboardSettingsForm_Shown);
}
}
I am not sure why if I comment out: this.MdiParent = _mForm;
the form will show(but not as a mdichild). Leaving that code intact, the form refuses to show. How do I get that form to show as a mdichild?
Updated Working Code
public partial class mainForm : Form
{
private NavigationForm _navForm;
public mainForm()
{
InitializeComponent();
this.Shown += new System.EventHandler(this.mainForm_Shown);
}
private void mainForm_Shown(object sender, EventArgs e)
{
_navForm = new NavigationForm(this);
_navForm.MdiParent = this;
_navForm.Show();
}
private void mainForm_Load(object sender, EventArgs e)
{
}
}
public partial class NavigationForm : Form
{
private KeyboardSettingsForm _wKeyboard;
public NavigationForm(Form frm)
{
InitializeComponent();
_wKeyboard = new KeyboardSettingsForm(frm);
}
private void NavigationForm_Load(object sender, EventArgs e)
{
}
private void keyboardPictureBox_Click(object sender, EventArgs e)
{
_wKeyboard.Show();
}
}
public partial class KeyboardSettingsForm : Form
{
private Form _mdiParent;
public KeyboardSettingsForm(Form frm)
{
InitializeComponent();
_mdiParent = frm;
this.MdiParent = frm;
this.Shown += new System.EventHandler(this.KeyboardSettingsForm_Shown);
}
private void KeyboardSettingsForm_Load(object sender, EventArgs e)
{
MessageBox.Show(_mdiParent.Name);
}
private void KeyboardSettingsForm_Shown(object sender, EventArgs e)
{
}
}
Upvotes: 1
Views: 1895
Reputation: 73442
You're saying who is KeyboardSettingsForm
's parent but where you're showing the parent?
_mForm = new mainForm(); //<---mdiparent not shown :(
this.MdiParent = _mForm;
Try this
_mForm = new mainForm();
_mForm.Show();//show your parent first
this.MdiParent = _mForm;
But even above code too makes only less sense. Did you mean to do something like this?
public partial class KeyboardSettingsForm : Form
{
private mainForm _mForm;
public KeyboardSettingsForm(mainForm mForm)
{
InitializeComponent();
this._mForm = mForm;//Did you mean this?
this.MdiParent = _mForm;
this.Shown += new System.EventHandler(this.KeyboardSettingsForm_Shown);
}
}
Upvotes: 1
Reputation: 17850
You need to show Main Form explicitly as follows:
_mForm = new mainForm();
this.MdiParent = _mForm;
this.Shown += this.KeyboardSettingsForm_Shown;
_mForm.Show(); // show mdi-parent explicitly because only the application's start-up form shows automatically.
Upvotes: 1
Reputation: 11577
you need to make mForm
an mdi container:
mForm.IsMdiContainer = true;
Upvotes: 1