Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从不同范围更改变量值_C#_Winforms - Fatal编程技术网

C# 从不同范围更改变量值

C# 从不同范围更改变量值,c#,winforms,C#,Winforms,如果我有Form1和Form2,并且我有事件按钮1\u单击Form1中的,这样: System.Void button1_Click(System.Object sender, System.EventArgs e) { static boolean x= false; .. .. Form2.Show(); } 我想在关闭Form2时,将x=true,如何做到这一点您需要将x更改为公共静态属性或字段x应该在类级别声明,以使其他类可见 Form2 otherForm = new Form2

如果我有Form1和Form2,并且我有事件
按钮1\u单击Form1中的
,这样:

System.Void button1_Click(System.Object  sender, System.EventArgs  e) {
static boolean x= false;
..
..
Form2.Show();
}

我想在关闭Form2时,将x=true,如何做到这一点

您需要将
x
更改为公共静态
属性
字段
x
应该在类级别声明,以使其他类可见

Form2 otherForm = new Form2();
bool x = false;
otherForm.FormClosing += (s,args)=> x = true;

然后,根据需要简单地更新FROM2的静态属性。

看起来像C++。但是看着“布尔”这个词,我开始怀疑了。根本不需要更改它的可见性,最好不要更改。不要在比必须的范围更大的范围内公开状态。