Home > database >  Variable wont store the textbox value
Variable wont store the textbox value

Time:02-22

What im trying to do is to transfer the variables from form2 to form5 and use those variables to replace already existing labels that is on form5

so what i tried is

//this is form 2
public string name;
public string surname;
name = textBox1.Text;
surname = textBox2.Text;
this.Hide();
var form3 = new Form3();
form3.Show();

//this is form 5
private void Form5_Load(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            label1.Text = form1.name;
            label2.Text = form1.surname;
        }

Default value of the labels on form 5 is "Placheholder" and for some reason they wont change into the variables what is the problem

CodePudding user response:

On Form1, set your strings to

public static string name;
public static string surname;

Then, in Form2

  string newSurname;
  string newName;
            public Form2()
            {
                InitializeComponent();
                newSurname = Form1.surname;
                newName = Form1.name;
            }
  • Related