Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Variable wont store the textbox value

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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;
            }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading