1
// Form1
// I have a text box and I want the value to pass onto a text box on form 2
private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2();
    form2.Show();
}



// Form2
private Form1 otherForm;
private void Form2_Load(object sender, EventArgs e)
{
    string test = textBox1.Text;
    otherForm.textBox1.Text = test;
}

When I try to pass the value onto the textBox on form two it says "Exception has been thrown by the target of an invocation."

I've changed the protection to public also but I'm having no success with passing the string along.

Alexander Manekovskiy
  • 3,185
  • 1
  • 25
  • 34
  • 1
    what is `otherForm` is it Form1? if not where are you creating it? – Mark Hall Dec 28 '13 at 19:38
  • Look at this thread: http://stackoverflow.com/questions/1806795/how-to-change-text-in-a-textbox-on-another-form-in-visual-c – CAPS LOCK Dec 28 '13 at 19:40
  • I forgot to add that in the op, sorry. I had private Form1 otherForm; – user3142612 Dec 28 '13 at 19:42
  • I've inlined your comment (next time please edit post instead of adding code in comments), but you still need to show how you are creating/initializing `otherForm` field. – Alexei Levenkov Dec 28 '13 at 19:47
  • 2
    Let me understand. You have Form1 with a textbox. The user opens Form2 clicking on a button of Form1. When loading Form2 you want to change the value of the textbox on Form1? It seems a bit strange because your user cannot change the value on form2 while it is still in load event – Steve Dec 28 '13 at 19:47
  • and why use the onloadevent when you could do it in your constructor – Softwarehuset Dec 28 '13 at 19:52
  • When the second form loads, I want the text box value from form1 to carry over to form2 – user3142612 Dec 28 '13 at 19:57
  • @user3142612 That is not what your code is showing, it showing a TextBox value from Form2 being sent to a Form1. – Mark Hall Dec 28 '13 at 20:10

5 Answers5

2

The easiest way to achieve this is to pass the information to the constructor of the second form. That way you don't need to make a textbox public.

In Form2:

// Constructor
public Form2 (string message)
{
    InitializeComponent();    // Call this first, as it creates the forms controls
    textBox1.Text = message;
}

In Form1:

private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2(textbox1.Text);
    form2.Show();
}

Btw, you are passing the string from Form2 to Form1 (the opposite way as required) and you are never initializing the variable otherForm. It will always have its default value null.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
1

As far as i understand your question you want to pass a value of textbox from form1 to form2.

In Form2 Add the following code.

    private Form1 otherForm;
    public Form2(Form1 formObj)
    {
     InitializeComponents();
     otherForm = formobj;
    }

    private void Form2_Load(object sender, EventArgs e)
    {            
        this.textbox1.Text = otherForm.textbox1.Text;
    }

In form1 button event add following code

   private void button1_Click(object sender, EventArgs e)
   {
     Form2 form2 = new Form2(this);
     form2.Show();
   }
shujaat siddiqui
  • 1,527
  • 1
  • 20
  • 41
1

When you open the Form2 you pass the value of the textbox to the constructor of Form2.
Inside the constructor of Form2 store the passed value in a global level class variable and use it in the Form2_Load event.
You could also set the textbox1 of Form2 directly in the constructor of Form2 but after the call to InitializeComponent

// In Form1
private void button1_Click(object sender, EventArgs e)
{
    Form2 form2 = new Form2(textbox1.Text);
    form2.Show();
}


// in Form2
public class Form2:Form
{
    private string textFromOtherForm;

    public void Form2(string fromForm1)
    {
         InitializeComponent();
         textFormOtherForm = fromForm1;
    }    
    private void Form2_Load(object sender, EventArgs e)
    {
        this.textBox1.Text = textFromOtherForm;
    }
}
Steve
  • 213,761
  • 22
  • 232
  • 286
0

Since textboxt1 doesnt exist in form2 it is not accessable.

You can as selman says make the textbox public and have a reference to form1.

but an even better way to do this would be using a string on a constructor

public Form2 (string theContent){
//your init here
}
Softwarehuset
  • 815
  • 4
  • 10
  • 1
    I disagree. The best option here is to set the value on Form 2 via a property or public method before calling show. – tsells Dec 28 '13 at 19:52
  • @Geek Why is that a better option? – Lasse V. Karlsen Dec 28 '13 at 19:53
  • @Geek disagree. If you do so there is a risk that your forget to set the necessary parameters and end up with failing code. – Softwarehuset Dec 28 '13 at 19:57
  • Nobody said this was a required parameter. Opening up forms like this are usually not a best practice either as you will most likely end up with multiple required values if done this way. – tsells Dec 28 '13 at 20:00
  • Also - if the value is not set - you can do parameter / value checking on your class to ensure it's correct. What if I initialize the class with a null string? It will give you the same result as not setting it at all later. – tsells Dec 28 '13 at 20:01
  • the last part with null is of cause one of the flaws and as you mentioned always check to be sure the value is usable, but i would still say the risk was smaller than setting the value after the form has been created – Softwarehuset Dec 28 '13 at 20:10
0

I would caution developing this way as it leads to coupled code between display elements (forms). This is usually handled with a controller object (look up MVC (WEB) / MVVM (WPF)). That being said here are two ways you can do it.

Option 1 - Add a property

    public string Form1Text1Text { get; set; }

    public Form2()
    {
        InitializeComponent();
    }


   private void button1_Click(object sender, EventArgs e)
    {
        var f2 = new Form2();
        f2.Form1Text1Text = textBox1.Text;
        f2.ShowDialog();
    }

Option 2 - Call a method

 private string _mytext;
    public void SetText(string text)
    {
        _mytext = text;
    }



    private void button1_Click(object sender, EventArgs e)
    {
        var f2 = new Form2();
        f2.SetText(textBox1.Text);
        f2.ShowDialog();
    }
tsells
  • 2,751
  • 1
  • 18
  • 20
  • Note that in each of the examples above you would then set whatever display elements on Form 2 via the value (property or private variable) in your Loaded / Shown event handler for Form 2. – tsells Dec 28 '13 at 20:10