Krzysztof Szczepankiewicz February 2016

Form not showing the variable from previous form

I got five forms that show one by one: Form1 > Form2 > Form3 > Form4 > FinalForm.

Every one has a textbox and a Next button. My problem is that I can't get the variable from the textbox in Form1 to show it at FinalForm (the last one) in the label6. What is the problem?

Form1:

namespace DaneUzytkownika2
{
public partial class Form1 : Form
{
    Nazwisko nazwisko = new Nazwisko();
    private string imie;

   public string UstawImie
    {
        get
        {
            return imie;
        }
        set
        {
            imie = value;
        }
    }

    public Form1()
    {
        StartPosition = FormStartPosition.CenterScreen;
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        UstawImie = textBox1.Text;
        Hide();
        nazwisko.ShowDialog();
    }
}
}

Last form:

namespace DaneUzytkownika2
{
    public partial class KoncowaInformacja : Form
    {
        public KoncowaInformacja()
        {
            StartPosition = FormStartPosition.CenterScreen;
            InitializeComponent();
        }

        private void KoncowaInformacja_Load(object sender, EventArgs e)
        {
            Form1 form1 = new Form1();
            label6.Text = form1.UstawImie;
        }
    }
}

Answers


Luaan February 2016

You're creating a whole new instance of Form1. Why would you ever expect this is going to contain data from a completely different instance of Form1? If you want to pass data from one form to another, just pass them. For example, as an argument to the constructor, or by making your own ShowDialog method.

private void button1_Click(object sender, EventArgs e)
{
    var form = new Nawisko(textBox1.Text);

    ...
}

Post Status

Asked in February 2016
Viewed 2,198 times
Voted 14
Answered 1 times

Search




Leave an answer