Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 forms, Form1 and Form2.

In Form1 i have textbox, linklabel and button. When i click on linklabel it opens Form2.

In Form2 i will take input from textbox and store it in the variable 'str'. When i click on button in Form2, it should close Form2 and update the textbox in Form1 with the value in 'str'.

C#
public partial class Form2: Form
    {
 private void button1_Click(object sender, EventArgs e)
        {
string str=textbox1.Text;
// code here to set the textbox value in Form1
this.close();
}
Posted
Comments
Nirav Prabtani 17-Jul-13 5:55am    
You mean both textboxes are on diff. pages, right??
[no name] 17-Jul-13 6:11am    
Pass the string through the Form2 constructor.
Dheeraj_Gupta 17-Jul-13 6:12am    
You are working with windows app or web app...
SrinivasTiru 17-Jul-13 7:25am    
windows app

Write a property in Form1 which holds the value of textbox like
C#
private string _busnamevar = string.Empty;
 public string Busnamevar
 {
     get
     {
         return _busnamevar;
     }
     set
     {
         _busnamevar = value;
         textBox1.Text = Busnamevar;
     }
 }


Button click event in Form1
C#
private void button1_Click(object sender, EventArgs e)
{
    Form2 fr = new Form2(this);
    fr.Show();
}


Declare a variable in Form2 like
C#
private Form1 form1;


From2 constructor
C#
public Form2(Form1 f)
{
    form1 = f;
    InitializeComponent();
}


button click in Form2
C#
private void button1_Click(object sender, EventArgs e)
{
    string str = textBox1.Text;
    // code here to set the textbox value in Form1
    form1.Busnamevar = str;
    this.Close();

}
 
Share this answer
 
Comments
SrinivasTiru 18-Jul-13 0:05am    
Thank you..It solved my problem.
You can show the Form2 as model dialog having parent Form1, and when closing the Form2 set DialogResult. Check DialogResult in Form1 and update textbox of Form1.
Below is the code:

C#
public partial class Form1 : Form
   {
       public static string str1;
       public Form1()
       {
           InitializeComponent();
       }

       private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
       {
           Form2 form2 = new Form2();
           if(form2.ShowDialog()==DialogResult.OK)
           {
               textBox1.Text = str1;
           }
       }
   }


Form 2

C#
public partial class Form2 : Form
    {
        public string str;
        public Form2()
        {
            InitializeComponent();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            str = textBox1.Text;
            Form1.str1 = str;
            this.DialogResult = DialogResult.OK;
            this.Close();

        }
    }
 
Share this answer
 
Comments
SrinivasTiru 17-Jul-13 7:43am    
In Form2 when i click on button the value of the string in inserting into the textbox in Form1 but Form2 is not closing when i click on the button.
you can pass the string to constructor of Form1,like this:
C#
public partial class Form2: Form
    {
 private void button1_Click(object sender, EventArgs e)
        {
string str=textbox1.Text;
code here to set the textbox value in Form1
this.close();
Form1 f1=new Form1(str);
f1.Show();
}


public partial class Form1: Form
{
   public Form1(String str)
     {
        this.textbox1.Text=str;// textbox in Form1
     }
}

i think it'll work for you.
 
Share this answer
 
you can pass the string to constructor of Form1,like this:
C#
public partial class Form2: Form
    {
 private void button1_Click(object sender, EventArgs e)
        {
string str=textbox1.Text;
code here to set the textbox value in Form1
this.close();
Form1 f1=new Form1(str);
f1.Show();
}


public partial class Form1: Form
{
   public Form1(String str)
     {
        this.textbox1.Text=str;// textbox in Form1
     }
}

i think it'll work for you.
 
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