简体   繁体   English

如何等待 form2 完成?

[英]How can I wait for form2 to finish?

I have a WinForms app that needs input from the user in Form2.我有一个 WinForms 应用程序,需要 Form2 中的用户输入。 The app needs to wait for form2 to close.该应用程序需要等待 form2 关闭。 What code would I need to use in order for Form1 to wait for the other form to close?为了让 Form1 等待另一个表单关闭,我需要使用什么代码?

form1表格1

public static int R1 = 0;
public static int G2 = 0;
public static int B3 = 0;
menuStrip1.BackColor = Color.FromArgb(100, R1, G2, B3);
panel1.BackColor = Color.FromArgb(100, R1, G2, B3);

form2表格2

Form1.R1 = hScrollBar1.Value;
Form1.G2 = hScrollBar2.Value;
Form1.B3 = hScrollBar3.Value;

this.Close();

Thanks in advance提前致谢

There are two ways to open another form有两种方法可以打开另一个表单

var  frm = new MyForm();
frm.Show();
DoSomething();

and

var frm = new MyForm();
frm.ShowDialog();
DoSomething();

The first variant opens the other form and then immediately executes DoSomething() , while as the second variant with ShowDialog() opens the other form and waits until the other form has closed, before executing DoSomething() .第一个变体打开另一个表单,然后立即执行DoSomething() ,而作为带有ShowDialog()的第二个变体打开另一个表单并等到另一个表单关闭,然后再执行DoSomething()

Simply use frm.ShowDialog();只需使用frm.ShowDialog(); instead of frm.Show();而不是frm.Show(); . .


ShowDialog() also returns an enum value of the type DialogResult . ShowDialog()还返回DialogResult类型的enum值。 You can use this result for typical dialog forms having OK and Cancel buttons like this您可以将此结果用于典型对话框 forms 具有这样的OKCancel按钮

var frm = new MyForm();
if (frm.ShowDialog(this) == DialogResult.OK) {
    // OK button pressed
} else {
    // Cancel button pressed
}

Buttons have a DialogResult Property you can set in the properties window, which will automatically be returned by ShowDialog .按钮有一个DialogResult 属性,您可以在属性 window 中设置,该属性将由ShowDialog自动返回。

In the dialog form you can assign these buttons to the AcceptButton and CancelButton properties.在对话框表单中,您可以将这些按钮分配给AcceptButtonCancelButton属性。 This allows you to activate the OK-button with Enter and the Cancel-button with Esc .这允许您使用Enter激活 OK 按钮,使用Esc激活 Cancel 按钮。


You can pass the actual form with this to both, Show(this) and ShowDialog(this) .您可以将带有this的实际表单传递给Show(this)ShowDialog(this) This has the effect that the dialog form stays on top of the actual form.这具有对话框表单保持在实际表单之上的效果。

why dont you use a class and define these variables in it then call a function in form1 to check these variables every time needed then in form2 you can change the value of these variables and set the flag to true.为什么不使用 class 并在其中定义这些变量,然后在 form1 中调用 function 以在每次需要时检查这些变量,然后在 form2 中您可以更改这些变量的值并将标志设置为 true。 lets say we have a class called: "ColorRbg"假设我们有一个名为“ColorRbg”的 class

    public static int R1;
    public static int B1;
    public static int G1;
    public static bool Flag1;

use this flag to determine the value change in class.使用此标志来确定 class 中的值变化。 then define a function like "CheckChange" in this function you can you 2 loops the with conditions if flag is high then change and then set the flag to low and else if the flag is false then do the parts of code you want to be execute.然后在此 function 中定义一个 function 之类的“CheckChange”,如果标志为高,则可以进行 2 次循环,然后将标志设置为低,否则如果标志为假,则执行您要执行的代码部分.

Have a static bool in your form1 class called something like: form2closed在您的表单1 class 中有一个 static 布尔值,类似于: form2closed

Set this to false originally.最初将此设置为 false。

Create a new FormClosing event in form2, which sets this static variable to be true.在 form2 中创建一个新的 FormClosing 事件,将这个 static 变量设置为 true。

In the form1, when you have to wait for form2 to close, simply run a while loop checking the boolean.在 form1 中,当您必须等待 form2 关闭时,只需运行一个 while 循环检查 boolean。

Like this:像这样:

public class Form1 
{
   public static bool form2closed = false;
   ...
   public void ChangeColours()
   {
       while (!form2closed) { }
       menustrip1.backcolour = ...
   }
   ...
}
public class Form2
{
   public Form2
   {
       ...
       this.FormClosing += Form2_FormClosing;
   }
   private void Form2_FormClosing(object sender, FormClosingEventArgs e)
   {
       Form1.form2closed = true;
   }
   ...
}

Where... is your existing code.哪里...是您现有的代码。

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

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