更改表单时丢失变量值

时间:2013-08-12 20:58:19

标签: c# winforms

我有一个非常简单的程序,以帮助弄清楚如何在C#中使用多个表单。我有Form1( form1 )和Form2( form2 )。在form1上我有一个按钮,一个标签和一个串口。在form2上,我有一个按钮和一个标签。该程序的作用是,当我单击它关闭的按钮时,该窗体打开另一个,更改标签中的文本,然后更改BaudRate。这是form1的代码:

public partial class Form1 : Form
{
    //Making a refernce of Form2 called 'form2'.
    Form2 form2 = new Form2();

    public Form1()
    {
        InitializeComponent();
    }
    public void button1_Click(object sender, EventArgs e)
    {
        //Able to reference form2 in a style that replicated VB.NET
        form2.Show();
        this.Hide();
        form2.label2.Text = ("Hello2");
    }

    public void Form1_Load(object sender, EventArgs e)
    {
        label1.Text = ("Start!");
        ApplicationPort.BaudRate = 200;
    }

以下是form2的代码:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }
    public void button1_Click(object sender, EventArgs e)
    {
        //Declaring the new instance of Form1 called 'form1'.
        var form1 = new Form1();     
        this.Hide();
        form1.Show();
        form1.label1.Text = ("hello");
        MessageBox.Show(form1.ApplicationPort.BaudRate.ToString());
    }

    public void Form2_Load(object sender, EventArgs e)
    {
        //Declaring the new instancce for Form1 called 'form1'.
        var form1 = new Form1();
        MessageBox.Show(form1.ApplicationPort.BaudRate.ToString());

    }
}

所以正在发生的事情是,当我启动程序时,BaudRate 200 。当我单击按钮,第二个表单打开时,在加载事件中,我有一个消息框,显示BaudRate为 9600 ,默认值。然后当我单击form2中的按钮时,消息框显示 200 的原始BaudRate。为什么Load事件处理程序没有获取值?我在写引用,还是错误的?我正在使用Visual Studio 2010 Express WinForms。

3 个答案:

答案 0 :(得分:2)

    var form1 = new Form1();
    MessageBox.Show(form1.ApplicationPort.BaudRate.ToString());

通过此操作,您创建了Form1实例,该实例将所有设置都设置为默认值。

您需要将现有的Form1对象实例传递给新创建的Form2对象。

public partial class Form2 : Form
{
    Form1 form1;
    public Form2(Form1 frm)
    {
        form1 = frm;
        InitializeComponent();

    }
    public void button1_Click(object sender, EventArgs e)
    {
        //Declaring the new instance of Form1 called 'form1'.
        //var form1 = new Form1();     
        this.Hide();
        form1.Show();
        form1.label1.Text = ("hello");
        MessageBox.Show(form1.ApplicationPort.BaudRate.ToString());
    }

    public void Form2_Load(object sender, EventArgs e)
    {
        //Declaring the new instancce for Form1 called 'form1'.
        MessageBox.Show(form1.ApplicationPort.BaudRate.ToString());

    }
}

public partial class Form1 : Form
{
    //Making a refernce of Form2 called 'form2'.
    Form2 form2; // Pass the instance of this object to Form2!

    public Form1()
    {
        form2 = new Form2(this)
        InitializeComponent();
    }
    public void button1_Click(object sender, EventArgs e)
    {
        //Able to reference form2 in a style that replicated VB.NET
        form2.Show();
        this.Hide();
        form2.label2.Text = ("Hello2");
    }

    public void Form1_Load(object sender, EventArgs e)
    {
        label1.Text = ("Start!");
        ApplicationPort.BaudRate = 200;
    }

答案 1 :(得分:1)

您正在从Form2创建Form1的新实例。您可以将Form1传递给Form2构造函数。

public partial class Form2 : Form
{
    Form1 form1;

    public Form2(Form1 form1)
    {
        InitializeComponent();
        this.form1 = form1;
    }

    public void button1_Click(object sender, EventArgs e)
    {    
        this.Hide();
        form1.Show();
        form1.label1.Text = ("hello");
        MessageBox.Show(form1.ApplicationPort.BaudRate.ToString());
    }
}

答案 2 :(得分:1)

为了保存波特率值,您必须声明static属性,如下所示:

public static int baudRate = 200;

在包含SerialPort控件的表单中。

然后在表单构造函数中使用buadRate分配给SerialPort