将 ShowDialog() 替换为 Show()

我有一个使用ShowDialog()显示的表单,因此它是一个模态窗口。


private void OpenForm(object sender, ItemClickEventArgs e)

{

    MyForm testForm = new MyForm();

    ...

    testForm.Enabled = true;

    testForm.ShowDialog(this);

    var dialogOk = testForm.DialogOK;

    if(dialogOk)

    {

       //do some stuff 1

    }

}

表单上有一个“确定”按钮。单击“确定”时,对话框“为 true”。在 MyForm 类中:


private void OkClick(object sender, EventArgs e)

{

   // do some stuff 2

   ... 

   DialogOK = true;

   Hide();

}

我需要将其转换为非模式窗口。解决方案似乎是使用Show()而不是ShowDialog(),但是当我使用Show()时,代码不会停止并等待单击“确定”按钮,因此永远不会调用“做一些事情1”。


使用 Show(),我如何在单击“确定”按钮后保持“做一些事情 1”的行为?


更新:这是我现在正在尝试的:


public partial class MyForm: XtraForm

{

   public bool DialogOk;


   private void OkClick(object sender, EventArgs e)

   {

      // do some stuff 2

      ... 

      DialogOk = true;

      Close();

   }

}

方法 1:


public partial class MyMainForm : XtraForm

{

   private MyForm testForm;


   private void OpenForm(object sender, ItemClickEventArgs e)

    {

        if(testForm == null)

        {

            testForm = new MyForm();

        }

        ...

        testForm.Enabled = true;

        testForm.FormClosed += (s, a) => {

            var dialogOk = testForm.DialogOk;

            if (dialogOk)

            {

                // do some stuff 1

            }

        };

        testForm.Show(this);

    }

}

方法 2:


public partial class MyMainForm : XtraForm

{

       private MyForm testForm;


       private void OpenForm(object sender, ItemClickEventArgs e)

        {

            if(testForm == null)

            {

                testForm = new MyForm();

            }

            ...

            testForm.FormClosed += testForm_Closed;

            testForm.Show(this);

        }


        private void testForm_Closed(object sender, EventArgs args)

        {

            var testForm = (Form)sender;

            testForm.Closed -= testForm_Closed;


            if (testForm.DialogResult == DialogResult.OK)

            {

               // do some stuff 1

            }

        }

 }


墨色风雨
浏览 111回答 4
4回答

UYOU

您可以处理以下事件:Form.ClosedMyForm testForm = new MyForm();testForm.Closed += testForm_Closed;testForm.Show();private void testForm_Closed(object sender, EventArgs args){    var testForm = (Form)sender;    testForm.Closed -= testForm_Closed;    if (testForm.DialogResult == OK)        // do some stuff 1}

偶然的你

最简单的方法是将代码从 移动到事件处理程序 。但是,如果由于您可能希望对不同的任务使用相同的窗体,因此这不是放置代码的好地方,则可以为事件添加一个处理程序,该处理程序在窗体关闭并运行代码后调用,例如:OpenFormOkClickFormClosedprivate void OpenForm(object sender, ItemClickEventArgs e){    MyForm testForm = new MyForm();    ...    testForm.Enabled = true;    testForm.FormClosed += (s, a) => {      var dialogOk = testForm.DialogOK;      if(dialogOk)      {         //do some stuff 1      }    };    testForm.Show(this);}

12345678_0001

您可以使用绑定到 TaskCompletionSource 的异步事件处理程序,该处理程序侦听并等待表单的关闭private asyc void OpenForm(object sender, ItemClickEventArgs e) {&nbsp; &nbsp; var source = new TaskCompletionSource<DialogResult>();&nbsp; &nbsp; EventHandler handler = null;&nbsp; &nbsp; handler = (s, args) => {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; var&nbsp; form = (MyForm)s;&nbsp; &nbsp; &nbsp; &nbsp; form.FormClosed -= handler;&nbsp; &nbsp; &nbsp; &nbsp; source.SetResult(form.DialogResult);&nbsp; &nbsp; }&nbsp; &nbsp; var testForm = new MyForm();&nbsp; &nbsp; testForm.FormClosed += handler; //subscribe&nbsp; &nbsp; //...&nbsp; &nbsp; testForm.Enabled = true;&nbsp; &nbsp; testForm.Show();&nbsp; &nbsp; var dialogOk = await source.Task;&nbsp; &nbsp; if(dialogOk == DialogResult.Ok) {&nbsp; &nbsp; &nbsp; &nbsp;//do some stuff 1&nbsp; &nbsp; }}这样,您就可以将逻辑保留在 中,并允许代码在不阻塞的情况下等待。OpenForm在表单中,单击按钮时,您需要做的就是设置对话框结果并关闭表单。public partial class MyForm: XtraForm {&nbsp; &nbsp; //...&nbsp; &nbsp; private void OkClick(object sender, EventArgs e) {&nbsp; &nbsp; &nbsp; &nbsp; // do some stuff 2&nbsp; &nbsp; &nbsp; &nbsp; // ...&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; DialogResult = DialogResult.Ok;&nbsp; &nbsp; &nbsp; &nbsp; Cose();&nbsp; &nbsp; }}

回首忆惘然

这对我有用,所以不知道为什么它不适合你(挠头)......此窗体有两个按钮,一个按钮再次打开同一窗体,另一个按钮关闭窗体。“父”窗体将事件添加到“已关闭”事件。public partial class Form1 : Form{&nbsp; &nbsp; public Form1()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; InitializeComponent();&nbsp; &nbsp; }&nbsp; &nbsp; private void button1_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Form1 test = new Form1();&nbsp; &nbsp; &nbsp; &nbsp; test.FormClosed += Test_FormClosed;&nbsp; &nbsp; &nbsp; &nbsp; test.Show();&nbsp; &nbsp; }&nbsp; &nbsp; private void Test_FormClosed(object sender, FormClosedEventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("closed -- do something else here!");&nbsp; &nbsp; }&nbsp; &nbsp; private void button2_Click(object sender, EventArgs e)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Close();&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP