1

我已经设计了一堆 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)
    {

    }
}
4

3 回答 3

1

你需要制作mForm一个 mdi 容器:

mForm.IsMdiContainer = true;
于 2013-09-03T14:28:35.487 回答
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.
于 2013-09-03T14:36:19.643 回答
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);

    }
}
于 2013-09-03T14:36:30.083 回答