简体   繁体   中英

How to open a Form Dialog from a MDI child form (MDI application)

I have a MDI main form, a menu item that shows a child form (let's call it frmEmployees ), inside this form a Button (btnNew...), how do I open from here a form as Dialog ( frmNewEmployee ); I mean, frmEmployees can't be reached until frmNewEmployee has been closed.

// Main_Form_Load
Main_Form.IsMdiContainer = true;

From a menu item in main form, I open frmEmployees

// MenuItem_Click
frmEmployees frmEmp = new frmEmployees();
frmEmp.MdiParent = this;
frmEmp.Show();

from a Button, I open the another form

// newButton_Click
frmNewEmployee frmNE = new frmNewEmployee();
frmNE.MdiParent = this.MdiParent;
//frmNE.Show();      // OK, but allows return to frmEmployees
frmNE.ShowDialog();  // here comes the problem

Is there any method to block frmEmployees while frmNewEmployee is open?

Thanks in advance!

Don't set frmNE.mdiParent. Let the instance be a child of frmEmployees. To restate, don't set the mdiParent property and call frmNE.ShowDialog() and the blocked form will be frmEmployee.

namespace ModalTest
{
    using System;
    using System.Windows.Forms;

    public partial class frmMain : Form
    {
        frmEmployees frmEmp = new frmEmployees();
        frmNewEmployee frmNE = new frmNewEmployee();

        public frmMain()
        {
            InitializeComponent();
            IsMdiContainer = true;
        }

        private void btnGo_Click(object sender, EventArgs e)
        {
            frmEmp.MdiParent = this;
            frmEmp.Show();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            frmNE.MdiParent = frmEmp.MdiParent;
            frmEmp.Hide();
            frmNE.Show();
        }
    }
}

Essentially what I did is assign the third form frmNE to the parent of the second form frmEMP , then use frmEmp.Hide() to hide the form from view. Using '.ShowDialog()', as I mentioned above, forces your third form to become modal, and thus requiring it to be satisfied before execution can continue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM