AIDEN IV
AIDEN IV

Reputation: 79

How can I move back and forth between two forms without loosing entered data

i have two forms which i want to move back and forth with without loosing the data that i have entered on both form, when i go back to form1 from form2 the data remains in form1, but when i go to form2 in which i have entered data before, the data are all gone, is there a solution to this?

first form:

public userform1()
{
    InitializeComponent();
}

private void jThinButton1_Click(object sender, EventArgs e)
{
    userform2 form2 = new userform2();
    form2.Show();

    this.Hide();
    form2.Hide();
    form2.ShowDialog();
    this.Show();

second form:

private void jThinButton3_Click(object sender, EventArgs e)
{
    this.DialogResult = DialogResult.OK;
}

going back to form2 from form1 works fine, but the problem is I loose the data I have entered in form2 when I click next in form1, I want to keep the entered data in form 2, is it possible?

Upvotes: 0

Views: 529

Answers (1)

djv
djv

Reputation: 15774

Encapsulate your userform2 instance in a readonly property which creates a new one if it's not created already

private userform2 _form2;
private userform2 form2
{
    get
    {
        if (_form2 == null)
            _form2 = new userform2();
        return _form2;
    }
}

Then use it like this

this.Hide();
form2.ShowDialog();
this.Show();

now whenever you access form2 its the same instance of userform2.

Or simply if you want to use the field only, but the instance is created when userform1 is constructed.

private userform2 form2 = new userform2();

Upvotes: 2

Related Questions