我已经设计了一堆 mdichild 表单,并希望将这些表单显示为 mdichild。我将主表单设置为 mdi,并且能够正确地将其中一个表单显示为 mdichild。给我带来麻烦的代码是:
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);
}
}
我不确定为什么要注释掉:this.MdiParent = _mForm;
表单会显示(但不会显示为 mdichild)。保持该代码完整,表单拒绝显示。如何让该表格显示为 mdichild?
更新的工作代码
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)
{
}
}