Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created two forms: Form1, Form2.
On Form1 I have a new button and textbox. when we click on new button Form2 open and on Form2 we put a value and save it to a database .

Now I want to get Form2 textbox value on Form1 textbox after closing of Form2.
how to do this...?

Please help...
Posted
Updated 23-Sep-12 23:22pm
v2

If you are creating Windows Application:

Create Global Variable :
string studentName;

Form2 (Set variable's value in code)
studentName = "Your Name";

Form1 (Get variable's value)
textbox1.text = studentName;

refer following link Variable and Method Scope http://msdn.microsoft.com/en-us/library/ms973875.aspx[^]

Hope this helps !!
 
Share this answer
 
v2
C#
public partial class Form1 : Form
   {
      public Form1()
      {
         InitializeComponent();
      }

      private void button1_Click(object sender, EventArgs e)
      {
         Form2 ChildFrm = new Form2(this);
         ChildFrm.ShowDialog();
      }
   }


C#
public partial class Form2 : Form
   {
      private Form1 ParentForm;
      public Form2(Form1 parentForm)
      {
         InitializeComponent();
         ParentForm = parentForm;
      }

      private void Form2_FormClosing(object sender, FormClosingEventArgs e)
      {
         ParentForm.Controls["ParentFormTextBox"].Text = ChildTextBox.Text;
      }
   }
 
Share this answer
 
Try this one.
Form 1
C#
namespace form1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            Form2 frm = new Form2(textBox1);
            frm.Show();
        }
    }
}


Form 2
C#
namespace form1
{
    public partial class Form2 : Form
    {
        TextBox txtForm1;
        public Form2()
        {
            InitializeComponent();
        }
        public Form2(TextBox txt)
        {
            InitializeComponent();
            txtForm1 = txt;
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            txtForm1.Text = "hello world";
        }
    }
}
 
Share this answer
 
Hi,

Solved in the Solution 1

Passing values one form to another form in simple method[^]

Thanks & Regards,
Raghu
 
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