Igor
Igor

Reputation: 13

How can I wait for form2 to finish?

I have a WinForms app that needs input from the user in Form2. The app needs to wait for form2 to close. What code would I need to use in order for Form1 to wait for the other form to close?

form1

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

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

this.Close();

Thanks in advance

Upvotes: 1

Views: 1337

Answers (3)

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112382

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().

Simply use frm.ShowDialog(); instead of frm.Show();.


ShowDialog() also returns an enum value of the type DialogResult. You can use this result for typical dialog forms having OK and Cancel buttons like this

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.

In the dialog form you can assign these buttons to the AcceptButton and CancelButton properties. This allows you to activate the OK-button with Enter and the Cancel-button with Esc.


You can pass the actual form with this to both, Show(this) and ShowDialog(this). This has the effect that the dialog form stays on top of the actual form.

Upvotes: 3

Fumbleless
Fumbleless

Reputation: 28

Have a static bool in your form1 class called something like: form2closed

Set this to false originally.

Create a new FormClosing event in form2, which sets this static variable to be true.

In the form1, when you have to wait for form2 to close, simply run a while loop checking the 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.

Upvotes: 0

Hamed Pourgholy
Hamed Pourgholy

Reputation: 39

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. lets say we have a class called : "ColorRbg"

    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. 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.

Upvotes: 0

Related Questions