Resolved Passing variables between one form and another

PDS8475

Active member
Joined
Jun 25, 2019
Messages
41
Programming Experience
Beginner
Hi

I have two forms
Form1
Welcome

Form1 has textbox called textBox2 and a button called LogIn_button
Welcome has a Label called Name_label

When I press the Login_button the text in textbox2 should be passed to the text of Name_label on the Welcome form
However Name_label shows Welcome instead of what I wrote in textbox2

I have the following code to pass the value to the Welcome form
C#:
private void LogIn_button_Click(object sender, EventArgs e)
        {

            if (textBox2.Text != "" && textBox2.Text != "Enter Name") {
                this.Hide();
                N = textBox2.Text;
                Welcome wel = new Welcome(N);
                wel.Show();
            }
        }
This seems to work, as if I put a breakpoint after the N=textBox2.Text line, I can see N equals what ever I wrote in textBox2

I have the following code in the Welcome form
C#:
public Welcome(String Name)
        {
            InitializeComponent();
        }
        private void Welcome_Load(object sender, EventArgs e)
        {

            string N = Name;
            Name_label.Text = N;
        }

Now if I put in a breakpoint, Name now equals Welcome

I really haven't got a clue why it would change
 
Last edited:
Okay I solved it. Apparently The line "Name_label.Text = N;" had to be changed to "Name_label.Text = Name;" and moved just after the "InitializeComponent();" line and the "string N = Name; line deleting
 
Back
Top Bottom