0

I am passing data between 2 windows forms in C#. Form1 is the main form, whose textbox will receive the text passed to it from form2_textbox & display it in its textbox (form1_textbox).

First, form1 opens, with an empty textbox and a button, on clicking on the form1_button, form2 opens. In Form2, I entered a text in form2_textbox & then clicked the button (form2_button).ON click event of this button, it will send the text to form1's textbox & form1 will come in focus with its empty form1_textbox with a text received from form2.

I am using properties to implement this task. FORM2.CS

public partial class Form2 : Form { //declare event in form 2 public event EventHandler SomeTextInSomeFormChanged;

public Form2()
{
    InitializeComponent();

}
public string get_text_for_Form1
{
    get { return form2_textBox1.Text; }
}

//On the button click event of form2, the text from form2 will be send to form1:

public void button1_Click(object sender, EventArgs e)
{
    Form1 f1 = new Form1();
    f1.set_text_in_Form1 = get_text_for_Form1;

//if subscribers exists
if(SomeTextInSomeFormChanged != null)
{
    SomeTextInSomeFormChanged(this, null);
}

}

}

FORM1.CS

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public string set_text_in_Form1
    {
        set { form1_textBox1.Text = value; }
    }

    private void form1_button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
        f2.SomeTextInSomeFormChanged +=new EventHandler(f2_SomeTextInSomeFormChanged);  
    }

    //in form 1 subcribe to event
    Form2 form2 = new Form2();

    public void f2_SomeTextInSomeFormChanged(object sender, EventArgs e)
    {
        this.Focus();

    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sqlchild
  • 8,754
  • 28
  • 105
  • 167
  • Form1 textbox's text is getting set in button click! Probably all you need is `form1.Focus()` in button click handler. – VinayC Feb 23 '11 at 07:10
  • [Duplicate 1](http://stackoverflow.com/questions/13465158/). [Duplicate 2](http://stackoverflow.com/questions/5087934/). [Duplicate 3](http://stackoverflow.com/questions/5088213/). [Duplicate 4](http://stackoverflow.com/questions/9073267/). [Duplicate 5](http://stackoverflow.com/questions/20186722/). – Dour High Arch May 21 '16 at 16:51

3 Answers3

3

In form2 you need to create event and subscribe to it in form1. Thats all.

//declare event in form 2
public event EventHandler SomeTextInSomeFormChanged;

// call event in form2 text_changed event
if(SomeTextInSomeFormChanged != null)
   SomeTextInSomeFormChanged(this, null);

//in form 1 subcribe to event
var form2 = new Form2();
form2.SomeTextInSomeFormChanged += SomeHandlerInForm1WhereYouCanSetForcusInForm1

Update:

Form2:

public Form2()
{
    InitializeComponent();
}

public void button1_Click(object sender, EventArgs e)
{
//if subscribers exists
  if(SomeTextInSomeFormChanged != null)
  {
    SomeTextInSomeFormChanged(form2_textBox1, null);
  }
}

Form1:

public partial class Form1 : Form { 

   public Form1() { InitializeComponent(); }

    private void form1_button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
        f2.SomeTextInSomeFormChanged +=new EventHandler(f2_SomeTextInSomeFormChanged);  
    }

    public void f2_SomeTextInSomeFormChanged(object sender, EventArgs e)
    {
        var textBoxFromForm2 = (TextBox)sender;
        form1_textBox1.Text =  textBoxFromForm2.Text
        this.Focus();
    }
}
Andrew Orsich
  • 52,935
  • 16
  • 139
  • 134
  • 1
    Thank you for not suggesting to pass a reference to the main form in the child form's constructor. However, I don't think your response is going to help the OP too much (they they are perfectly capable of looking up how to use events on their own). +1 – Ed S. Feb 23 '11 at 07:10
  • @Bugai13 : sir , how would i do this, can you give me the code please. – sqlchild Feb 23 '11 at 07:11
  • @Bugai13 : sir, the buton_Click is the created event, as i have mentioned in the code, but how to subscribe to it in form1 , please tell? – sqlchild Feb 23 '11 at 07:12
  • @Ed S. : sir, how i would i subscribe to the event? – sqlchild Feb 23 '11 at 07:16
  • @Bugai13: sir, its giving error, where should i write the form1 code which you have given, in form1_load event or where? and it says -----The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) – sqlchild Feb 23 '11 at 07:31
  • @Bugai13: i have posted the total code here , please rectify it---http://stackoverflow.com/questions/5088213/passing-data-between-two-forms-using-properties – sqlchild Feb 23 '11 at 07:45
  • DIN'T GET THE SOLUTION YET! ANYBODY PLEASE HELP – sqlchild Feb 23 '11 at 09:06
  • @Bugai13: sir , i din't get the solution – sqlchild Feb 23 '11 at 09:17
  • @sqlchild: sir, i've a little modified your code. Check my update. – Andrew Orsich Feb 23 '11 at 09:21
  • @Bugai13: sir, its still giving error, it says -----The type or namespace name 'var' could not be found (are you missing a using directive or an assembly reference?) – sqlchild Feb 24 '11 at 06:43
  • var it is .net 3.0 feature, if you using previous version of .net paste TextBox instead of var. – Andrew Orsich Feb 24 '11 at 07:13
2

The website listed below has very good tutorials. This particular page demonstrates how this can be achieved:

http://www.vcskicks.com/data-between-forms.php

0

What about this.

((Form2)Application.OpenForms["Form2"]).textBox1.Text = "My Message";
Aliaksei Kliuchnikau
  • 13,589
  • 4
  • 59
  • 72