• <bdo id='50pRi'></bdo><ul id='50pRi'></ul>
  • <small id='50pRi'></small><noframes id='50pRi'>

  • <tfoot id='50pRi'></tfoot>
    <i id='50pRi'><tr id='50pRi'><dt id='50pRi'><q id='50pRi'><span id='50pRi'><b id='50pRi'><form id='50pRi'><ins id='50pRi'></ins><ul id='50pRi'></ul><sub id='50pRi'></sub></form><legend id='50pRi'></legend><bdo id='50pRi'><pre id='50pRi'><center id='50pRi'></center></pre></bdo></b><th id='50pRi'></th></span></q></dt></tr></i><div id='50pRi'><tfoot id='50pRi'></tfoot><dl id='50pRi'><fieldset id='50pRi'></fieldset></dl></div>

      <legend id='50pRi'><style id='50pRi'><dir id='50pRi'><q id='50pRi'></q></dir></style></legend>

        WinForms 编程 - 模态和非模态表单问题

        WinForms programming - Modal and Non-Modal forms problem(WinForms 编程 - 模态和非模态表单问题)

          <tbody id='6s5Ms'></tbody>
          <i id='6s5Ms'><tr id='6s5Ms'><dt id='6s5Ms'><q id='6s5Ms'><span id='6s5Ms'><b id='6s5Ms'><form id='6s5Ms'><ins id='6s5Ms'></ins><ul id='6s5Ms'></ul><sub id='6s5Ms'></sub></form><legend id='6s5Ms'></legend><bdo id='6s5Ms'><pre id='6s5Ms'><center id='6s5Ms'></center></pre></bdo></b><th id='6s5Ms'></th></span></q></dt></tr></i><div id='6s5Ms'><tfoot id='6s5Ms'></tfoot><dl id='6s5Ms'><fieldset id='6s5Ms'></fieldset></dl></div>
            <tfoot id='6s5Ms'></tfoot>
              • <small id='6s5Ms'></small><noframes id='6s5Ms'>

                  <bdo id='6s5Ms'></bdo><ul id='6s5Ms'></ul>

                  <legend id='6s5Ms'><style id='6s5Ms'><dir id='6s5Ms'><q id='6s5Ms'></q></dir></style></legend>
                  本文介绍了WinForms 编程 - 模态和非模态表单问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我对 C#.NET 下的表单形式有疑问.假设我有主表单#0(见下图).此表单代表主应用程序表单,用户可以在其中执行各种操作.但是,有时需要打开额外的非模态表单来执行额外的主要应用程序功能支持任务.假设这是图像中的表格#1.在这个 #1 表单上,可能会打开几个额外的模态表单(图像中的 #2 表单),最后,有一个进度对话框显示较长的操作进度和状态,这可能需要几个几分钟到几个小时.问题是主窗体#0 在您关闭所有模式窗体(图像中的#2)之前没有响应.我需要主窗体#0 在这种情况下可以运行.但是,如果您在表单#2 中打开一个非模态表单,您可以使用模态#2 表单和新创建的非模态表单进行操作.我需要主窗体#0 和窗体#1 及其所有子窗体之间的相同行为.是否可以?还是我做错了什么?也许有某种解决方法,我真的不想将所有 ShowDialog 调用更改为 Show...

                  I have a problem with modality of the forms under C#.NET. Let's say I have main form #0 (see the image below). This form represents main application form, where user can perform various operations. However, from time to time, there is a need to open additional non-modal form to perform additional main application functionality supporting tasks. Let's say this is form #1 in the image. On this #1 form there might be opened few additional modal forms on top of each other (#2 form in the image), and at the end, there is a progress dialog showing a long operation progress and status, which might take from few minutes up to few hours. The problem is that the main form #0 is not responsive until you close all modal forms (#2 in the image). I need that the main form #0 would be operational in this situation. However, if you open a non-modal form in form #2, you can operate with both modal #2 form and newly created non modal form. I need the same behavior between the main form #0 and form #1 with all its child forms. Is it possible? Or am I doing something wrong? Maybe there is some kind of workaround, I really would not like to change all ShowDialog calls to Show...

                  图片 http://img225.imageshack.us/img225/1075/modalnonmodalproblem.png

                  推荐答案

                  模态表单完全符合模态"的含义,它们禁用应用程序中的所有其他窗口.这很重要,您的程序处于某种危险的状态.您有一大段代码正在等待对话框关闭.如果没有禁用其他窗口,可能会发生非常糟糕的事情.就像用户可以再次启动模式对话框一样,现在您的代码嵌套了两次.或者她可以关闭对话框的所有者窗口,现在它突然消失了.

                  Modal forms do exactly what "modal" means, they disable all other windows in the app. That's rather important, your program is in a somewhat perilous state. You've got a chunk of code that is waiting for the dialog to close. Really Bad Things could happen if those other windows were not disabled. Like the user could start the modal dialog again, now your code is nested twice. Or she could close the owner window of the dialog, now it suddenly disappears.

                  如果您在循环中调用 Application.DoEvents(),这些正是您会遇到的问题.这是在不禁用其他窗口的情况下让表单表现模态的一种方法.例如:

                  These are the exact kind of problems you'd run into if you call Application.DoEvents() inside a loop. Which is one way to get a form to behave modal without disabling other windows. For example:

                      Form2 mDialog;
                  
                      private void button1_Click(object sender, EventArgs e) {
                          mDialog = new Form2();
                          mDialog.FormClosed += (o, ea) => mDialog = null;
                          mDialog.Show(this);
                          while (mDialog != null) Application.DoEvents();
                      }
                  

                  这是危险的.

                  当然最好按照它们的设计方式使用模态表单以避免麻烦.如果您不想要模态表单,那么就不要将其设为模态,请使用 Show() 方法.订阅它的 FormClosing 事件以知道它即将关闭:

                  It is certainly best to use modal forms the way they were designed to stay out of trouble. If you don't want a modal form then simply don't make it modal, use the Show() method. Subscribe to its FormClosing event to know that it is about to close:

                      private void button1_Click(object sender, EventArgs e) {
                          var frm = new Form2();
                          frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
                          frm.Show();
                      }
                  
                      void frm_FormClosing(object sender, FormClosingEventArgs e) {
                          var frm = sender as Form2;
                          // Do something with <frm>
                          //...
                      }
                  

                  这篇关于WinForms 编程 - 模态和非模态表单问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)
                  <legend id='f1fuk'><style id='f1fuk'><dir id='f1fuk'><q id='f1fuk'></q></dir></style></legend>
                    • <bdo id='f1fuk'></bdo><ul id='f1fuk'></ul>

                          <tbody id='f1fuk'></tbody>

                        1. <tfoot id='f1fuk'></tfoot>

                            <i id='f1fuk'><tr id='f1fuk'><dt id='f1fuk'><q id='f1fuk'><span id='f1fuk'><b id='f1fuk'><form id='f1fuk'><ins id='f1fuk'></ins><ul id='f1fuk'></ul><sub id='f1fuk'></sub></form><legend id='f1fuk'></legend><bdo id='f1fuk'><pre id='f1fuk'><center id='f1fuk'></center></pre></bdo></b><th id='f1fuk'></th></span></q></dt></tr></i><div id='f1fuk'><tfoot id='f1fuk'></tfoot><dl id='f1fuk'><fieldset id='f1fuk'></fieldset></dl></div>
                          1. <small id='f1fuk'></small><noframes id='f1fuk'>