简体   繁体   English

在MDI容器中打开另一个表单

[英]Opening another form in an MDI container

Please refer to the diagram below: 请参考下图:

http://i.imgur.com/mMS2wDk.jpg

I'm looking for the best way from one form to open another form. 我正在寻找从一种形式打开另一种形式的最佳方法。 Once the menu form is open, I want to close the login form. 打开菜单表单后,我要关闭登录表单。 I have tried to do this, but I got lost when I try to call it as a child of the MDI container. 我曾尝试这样做,但是当我尝试将其称为MDI容器的子代时迷路了。

The reason your main form isn't showing is because once you close the login form, your application's message pump is shut down, which causes the entire application to exit. 无法显示主表单的原因是因为一旦关闭登录表单,应用程序的消息泵就会关闭,这将导致整个应用程序退出。 The Windows message loop is tied to the login form because that's the one you have set as the startup form in your project properties. Windows消息循环与登录表单相关联,因为这是您在项目属性中设置为启动表单的表单。 Look in your "Program.cs" file, and you'll see the responsible bit of code: Application.Run(new LoginForm()). 查看“ Program.cs”文件,您将看到负责的代码:Application.Run(new LoginForm())。 Check out the documentation for that method here on MSDN, which explains this in greater detail. 在MSDN上查看有关该方法的文档,其中对此进行了更详细的说明。

The best solution is to move the code out of your login form into the "Program.cs" file. 最好的解决方案是将代码从登录表单中移出到“ Program.cs”文件中。 When your program first starts, you'll create and show the login form as a modal dialog (which runs on a separate message loop and blocks execution of the rest of your code until it closes). 程序首次启动时,您将创建并以模式对话框形式显示登录表单(该对话框在单独的消息循环上运行,并阻止其余代码的执行,直到关闭为止)。 When the login dialog closes, you'll check its DialogResult property to see if the login was successful. 登录对话框关闭后,您将检查其DialogResult属性以查看登录是否成功。 If it was, you can start the main form using Application.Run (thus creating the main message loop); 如果是这样,则可以使用Application.Run启动主窗体(从而创建主消息循环)。 otherwise, you can exit the application without showing any form at all. 否则,您可以退出应用程序而无需显示任何形式。 Something like this: 像这样:

static void Main()
{
    LoginForm fLogin = new LoginForm();
    if (fLogin.ShowDialog() == DialogResult.OK)
    {
        Application.Run(new MainForm());
    }
    else
    {
        Application.Exit();
    }
}

if you develop WPF form, you can use ' http://wpfmdi.codeplex.com/ '. 如果您开发WPF表单,则可以使用“ http://wpfmdi.codeplex.com/ ”。 or in Win Form you could play with form such that you want 或在Win Form中,您可以使用自己想要的形式

try this 尝试这个

    //global form
    LoginForm login = new LoginForm();
    public void menuOpen()
    {
       if(login.Visible)
          login.Close();
    }

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

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