Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all
i m new to C# coding want to knw how we can hold the data in textbox in form1.while coming back from Form2.

Actually friends i m having two forms. in form1 i m having a Textbox and a button.
and in form2 i m having just a Button( to go back to form1).

in form1s button i hav written-
Form2 a = new Form2();
a.show();
This.Hide();

and in Form2 button i hav written
Form1 a = new Form1();
a.show();
This.Hide();

But whenver i clicked a Button in Form2. it starts Form1 as a new Form..means a Fresh Form...it is not showing me the Data in Textbox..Please try to resolve it.and write in a way so tht i can understand it..as i m new u knw..:P

and please understand my english i m not good in it. :P
Thanks,
Sudesh..
Posted

Ok so Solution 1 is for Asp.net and as I understand it this is a Windows Form App. So in that case the easiest way to do this is to just pass a reference from form1 to form 2 like so:

In form1:

C#
private void button1_Click(object sender, EventArgs e)
{
    Form2 a = new Form2(this);
    a.Show();
    this.Hide();
}


and in form2:
C#
public partial class Form2 : Form
{
    Form1 mainForm;

    public Form2(Form1 mainForm)
    {
        InitializeComponent();

        this.mainForm = mainForm;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        mainForm.Show();
        this.Hide();
    }
}


Also one more thing in Form2 calling this.Hide(); the form will still exist in memory it will only be hidden from the user if this is what you intended that that is fine. However if you want to dispose of the form2 call this.Close() instead.
 
Share this answer
 
v3
Use ViewState for that...for more detail and example check the below link

http://asp.net-tutorials.com/state/viewstate/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900