我已经设计了一堆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)
{
}
}
答案 0 :(得分:1)
您需要将mForm
设为mdi容器:
mForm.IsMdiContainer = true;
答案 1 :(得分:1)
您需要明确显示主表单,如下所示:
_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.
答案 2 :(得分:1)
你说的是KeyboardSettingsForm
的父母是谁,但你在哪里展示父母?
_mForm = new mainForm(); //<---mdiparent not shown :(
this.MdiParent = _mForm;
试试这个
_mForm = new mainForm();
_mForm.Show();//show your parent first
this.MdiParent = _mForm;
但即使是上面的代码也只是没有意义。你的意思是做这样的事吗?
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);
}
}