简体   繁体   中英

Pause event before another event finish

I have two Forms (Form1 and Form2). On Form1 is treeView control and bool variable YesNo and on Form2 is button.

What I want to do is run treeView1_NodeMouseDoubleClick and then run Form2, click button on that Form2 and then set value variable YesNo to true.

But I'm stuck because treeView1_NodeMouseDoubleClick keep running after Form2 is shown and I don't know how to stop until button1_Click event is finish.

From code below I would like to get MessegeBox with text True on it.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public static bool YesNo { get; set; }

    private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
    {
        if (treeView1.SelectedNode.Text == "GB")
        {
            Form2 f2 = new Form2();
            f2.Show();

            # how to stop this event until button1_Click is finish

            MessageBox.Show(YesNo.ToString());
        }
    }
}

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1.YesNo = true;            
    }
}

First problem at your code: theres no "YesNo" variable at any if() operator

Heres the way to fix it :

Replace your :

if (treeView1.SelectedNode.Text == "GB")

By:

if (treeView1.SelectedNode.Text == "GB" && YesNo == false)

Another problem: you cant call static variables from another form in cause of their high-level protection, the way to fix it:

Remove "static" variable type from variable

public static bool YesNo { get; set; }

Heres what should leave:

public bool YesNo { get; set; }

Third problem: How do you trying to call variable from Form1 without anouncing the Form1 at Form2 class?? By invisible server pipes XD???

Heres the way to fix it, its like you done it at your event inside Form1:

Write that to your button1_Click event at Form2:

Form1 f1 = new Form1();
f1.YesNo = true;

It should be like this:

private void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    f1.YesNo = true;
}

Without few empty fields that I've wrote.

Mark me higher if it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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