【问题标题】:Pause event before another event finish在另一个事件完成之前暂停事件
【发布时间】:2020-12-23 02:43:57
【问题描述】:

我有两个表格(Form1 和 Form2)。在 Form1 上是 treeView 控件和 bool 变量 YesNo,在 Form2 上是按钮。

我要做的是运行 treeView1_NodeMouseDoubleClick 然后运行 ​​Form2,单击 Form2 上的按钮,然后将值变量 YesNo 设置为 true。

但我被卡住了,因为在显示 Form2 后 treeView1_NodeMouseDoubleClick 继续运行,并且在 button1_Click 事件完成之前我不知道如何停止。

从下面的代码中,我想获得带有 True 文本的 MessegeBox。

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;            
    }
}

【问题讨论】:

  • 使用 ShowDialog 将其显示为模态窗口。
  • 很棒,很简单,但可以像我想要的那样工作。谢谢。

标签: c# winforms events


【解决方案1】:

代码中的第一个问题:任何 if() 运算符都没有“YesNo”变量

解决方法如下:

替换你的:

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

作者:

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

另一个问题:由于它们的高级保护,您不能从其他表单调用静态变量,解决方法:

从变量中删除“静态”变量类型

public static bool YesNo { get; set; }

这是应该留下的:

public bool YesNo { get; set; }

第三个问题: 您如何尝试从 Form1 调用变量而不在 Form2 类中宣布 Form1?通过不可见的服务器管道 XD???

这是修复它的方法,就像您在 Form1 内的活动中所做的一样:

将其写入 Form2 处的 button1_Click 事件:

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

应该是这样的:

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

没有我写的几个空字段。

如果有帮助,请给我加分

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多