从不同的表单中查找变量值

时间:2017-02-14 10:36:44

标签: c# .net winforms

我正在使用WinForms。我有2个表格,表格1(主表格)和表格2。我在Form1中有2个变量Computer_SelectedDefault_Selected。当我单击Form1中的按钮然后显示Form2时,我更改了一个变量。在Form2中我想知道变量bool值。我怎么能这样做?

表单1

    public bool Computer_Selected; 
    public bool Default_Selected;

    private void btn_Public_Kiosk_Click(object sender, EventArgs e)
    {
        Computer_Selected= true;
        Default_Selected = false;
        //show form2 and hide this form-(form1)...
    }

表格2

    private void Form1_Load(object sender, EventArgs e)
    {
         Console.WriteLine("Bool Value = " + Computer_Selected);
    }

1 个答案:

答案 0 :(得分:0)

如果您只有一个Form1实例,那么您可以将这些变量设置为static

public static bool Computer_Selected; 
public static bool Default_Selected;

然后在您的Form2中,您可以访问它们:

private void Form2_Load(object sender, EventArgs e)
{
     Console.WriteLine("Bool Value = " + Form1.Computer_Selected);
}