0

I have 2 forms, and the second form is meant to be active WHILE the first form is still active.

I need to pass the string value from textBox1.Text on Form1 to textBox1.Text on Form2.

How would such me done without reopening Form1 from Form2?

  • can you provide some cone snippet? – Rodrigo Aires Mar 26 '17 at 21:14
  • @RodrigoAires Such as what? I am kinda confused because only code I would relevant would be showing that the textboxes exist. ie: When someone types into textbox1 on Form1; it will submit it automatically into textBox1 on Form2 without refreshing Form2. – John Sulli Mar 26 '17 at 21:21
  • sorry, I'm confused about your need... are you talking about two different forms in the same window? – Rodrigo Aires Mar 26 '17 at 21:29

3 Answers3

0

Is this what you meant?

Form1 form1 = new Form1();
Form1 form2 = new Form2();

form1.Show();
form2.Show();

form2.Button1_Click += delegate {form2.textbox1.Text = form1.textbox1.Text};
Sergey Slepov
  • 1,861
  • 13
  • 33
  • No. I am opening form2 from from1, but form1 is not showing. And I want to pass a string from form2 back into form1 without reopening form1. – John Sulli Mar 26 '17 at 21:17
0

How about this?

class Form1 : Form
{
    void ButtonOpenForm2_Click(object s, EventArgs ea)
    {
        this.form2 = new Form2(this);
        this.form2.Show();
    }
}

class Form2 : Form
{
    public Form2(Form1 form1)
    {
        form1.textbox1.TextChanged += delegate {this.textbox1.Text = form1.textbox1.Text};
    }
}

static void Main()
{
    Form1 form1 = new Form1();

    form1.Show();
}
Sergey Slepov
  • 1,861
  • 13
  • 33
0
public class Form2 : Form
{
   //This property will hold the text, so populate the textbox from it   
   string TextProperty {get;set;}

   public form2(string textFromForm1)
   {
      TextProperty = textFromForm1;
   }
}

Now on form1:

   Form2 form2 = new Form2(textbox1.Text);
   form2.ShowDialog();
YazX
  • 444
  • 4
  • 12