简体   繁体   English

如何从 MDI 中的另一个子窗体打开子窗体?

[英]How to open the child form from another child form inside an MDI from?

I have an MDI form and 2 child forms What i want is, when i click on menu button in MDI, it should open the Login form, when the login is through, from the login form, on button click, I want to close the login form and open the second child form Here is the code i am trying, but it does not open inside the MDI, it opens outside.我有一个 MDI 表单和 2 个子表单我想要的是,当我点击 MDI 中的菜单按钮时,它应该打开登录表单,当登录通过时,从登录表单,点击按钮,我想关闭登录表单并打开第二个子表单 这是我正在尝试的代码,但它不在 MDI 内部打开,而是在外部打开。 Please help请帮忙

this.Close();
frmAdmin frm = new frmAdmin();
frm.MdiParent = this.ParentForm;
frm.Dock = DockStyle.Fill;
 frm.Show();

Rewrite the code as follows改写代码如下

frmAdmin frm = new frmAdmin();
frm.MdiParent = this.ParentForm;
this.close();
frm.Dock = DockStyle.Fill;
 frm.Show();

Do not call second form from Login form.不要从Login表单调用第二个表单。 Open Login form as modal dialog, and return login result to the parent form, which will "make decision" based on the login result and open second form.以模态对话框打开Login表单,并将登录结果返回给父表单,父表单将根据登录结果“做出决定”并打开第二个表单。

// Login function in main form
using (var login = new LoginForm())
{
    if (login.ShowDialog() == DialogResult.Ok)
    {
        var admin = new AdminForm
        {
            MdiParent = this;
            Dock = DockStyle.Fill;
        }

        admin.Show();
    }
    else
    {
        // do something when login fails
    }
}

With approach above your Login form will remain with only responsibility to login users.使用上述方法,您的Login表单将只负责登录用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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