Chase Ernst
Chase Ernst

Reputation: 1155

Losing variable value when changing forms

I have a very simple program, to help figure out how to use multiple forms in C#. I have Form1(form1), and Form2(form2). On form1 I have a button, a label, and a Serial Port. On form2 I have a button, and a label. What the program does, is when I click the button it closes that form opens the other, changes the text in the label, and then changes the BaudRate. Here is the code for 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;
    }

Here is the code for 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());

    }
}

So what is happening, is when I start the program the BaudRate is 200. When I click the button, and the second form opens, in the load event I have the message box showing the BaudRate as 9600, the default value. Then when I click the button in form2, the message box shows the original BaudRate of 200. Why is it that the Load event handler isnt getting the value? Am I writing the referencing, or something wrong? I am working with Visual Studio 2010 Express WinForms.

Upvotes: 1

Views: 1876

Answers (3)

user2389722
user2389722

Reputation:

In order to save the value of baud rate, you must declare a static property like this:

public static int baudRate = 200;

in your form which contains SerialPort control.

then use assign buadRate to SerialPort in your form constructor.

Upvotes: 1

Nemanja Boric
Nemanja Boric

Reputation: 22157

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

With this you are creating new instance of Form1 which will have all settings set to default values.

You need to pass your existing instance of Form1 object to the newly created Form2 object.

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

Upvotes: 1

Michael
Michael

Reputation: 1833

You are creating a new instance of Form1 from Form2. You could pass Form1 to the Form2 constructor.

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

Upvotes: 1

Related Questions