Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/293.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows窗体程序中的多个UI_C#_Winforms - Fatal编程技术网

C# Windows窗体程序中的多个UI

C# Windows窗体程序中的多个UI,c#,winforms,C#,Winforms,我有一个窗口窗体项目,我有一个登录屏幕,一个菜单和一些其他窗体,我在它们之间切换: this.Hide(); frm.FormClosed += new FormClosedEventHandler(subFormClosed); frm.Show(); 以及窗体ClosedEventHandler(子窗体Closed) 因此,这样做的目的是,当用户关闭子窗体时,关闭该子窗体 但是有一个问题,我想回到菜单,问题是我有两种可能,我可以看到: 我可以通过引用将菜单表单传递给子表单,然后显示和隐藏子

我有一个窗口窗体项目,我有一个登录屏幕,一个菜单和一些其他窗体,我在它们之间切换:

this.Hide();
frm.FormClosed += new FormClosedEventHandler(subFormClosed);
frm.Show();
以及
窗体ClosedEventHandler(子窗体Closed)

因此,这样做的目的是,当用户关闭子窗体时,关闭该子窗体

但是有一个问题,我想回到菜单,问题是我有两种可能,我可以看到:

  • 我可以通过引用将菜单表单传递给子表单,然后显示和隐藏子表单-这似乎是一种非常笨拙的方法,但它可以工作

  • 我可以打开菜单表单的新版本-这将导致过度使用的巨大内存问题(创建更多实例,然后在程序关闭之前从不销毁,例如30个菜单表单子表单)

  • 我试图使用CloseReason检查子表单是否由用户关闭,或者是否由代码关闭,但是退出按钮和
    this.Close()
    return
    CloseReason.UserClosing
    。因此,我无法区分这两种退出方式


    因此,基本上我要求的是有更好的方法来实现这一点,我已经阅读了有关MDI和SDI的内容,但我无法确定哪种方法适用,或者kludgy选项1是否是实现这一点的最佳方法。

    您可以使用ShowDialog并作为所有者传递菜单页。大概是这样的:

    在菜单中

    // on menu navigation button click
    this.hide();
    SubForm sub = new SubForm();
    sub.ShowDialog(this);   // open as a dialog with this form as the owner
    
    // on subform's back button click or better, in the FormClosing event
    this.Owner.show();
    this.Close();    // this line is not needed if implemented in FormClosing event
    
    在子表单中

    // on menu navigation button click
    this.hide();
    SubForm sub = new SubForm();
    sub.ShowDialog(this);   // open as a dialog with this form as the owner
    
    // on subform's back button click or better, in the FormClosing event
    this.Owner.show();
    this.Close();    // this line is not needed if implemented in FormClosing event
    

    尽管ShowDialog解决方案的答案非常好,但如果出于任何原因不想使用ShowDialog,这里还有另一种方法:

    在菜单窗体的构造函数中,按以下方式使用子窗体的
    FormClosed
    show
    事件:

    subForm1.FormClosed += (s, e) => showMenu();
    subForm1.Shown+= (s, e) => hideMenu();
    
    subForm2.FormClosed += (s, e) => showMenu();
    subForm2.Shown+= (s, e) => hideMenu();
    ...
    
    void showMenu()
    {
        this.Show();
    }
    
    void hideMenu()
    {
        this.Hide();
    }
    

    然后,您可以自由地使用subForm1.Show()并按您想要的方式关闭它们:事件将相应地触发。

    我认为最好的管理方法是真正关闭表单并在必要时调用它们(Show)——这是表单的正常用法。但是,如果您需要保持它们处于打开(甚至隐藏)状态,为什么不使用Minimize函数,而不使用“ShowInTaskBar”属性呢。这样,我相信你会达到你想要的——尽管我并不清楚。在你的例子中,“菜单”是什么形式?如果您想在其他窗体关闭时显示“菜单”窗体,为什么不能使用此处显示的相同技术?即,订阅
    FormClosed
    事件(从打开第一个“其他表单”的“菜单”代码),当“其他表单”关闭时,重新显示“菜单”表单。请提供清楚地显示您的问题,并精确地解释代码的作用以及它与您想要的不同之处。谢谢,这基本上是完美的