更改表单时丢失变量值

本文关键字:变量值 表单 | 更新日期: 2023-09-27 18:27:35

我有一个非常简单的程序,可以帮助了解如何在C#中使用多个表单。我有Form1(Form1)和Form2(Form2。在窗体1上,我有一个按钮、一个标签和一个串行端口。在窗体2上,我还有一个按钮和一个标签。该程序所做的是,当我单击按钮时,它会关闭该窗体,打开另一个窗体,更改标签中的文本,然后更改波特率。这是表格1:的代码

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

这是表格2的代码:

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

因此,当我启动程序时,波特率是200。当我单击按钮,打开第二个窗体时,在加载事件中,我会看到一个消息框,显示波特率为9600,即默认值。然后,当我单击表单2中的按钮时,消息框显示200的原始波特率。为什么Load事件处理程序没有得到值?我是在写参考资料,还是出了什么问题?我正在使用Visual Studio 2010学习版WinForms。

更改表单时丢失变量值

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

您正在从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());
    }
}

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

public static int baudRate = 200;

包含SerialPort控件的表单中。

然后在表单构造函数中使用assign buadRate to SerialPort