0

I have 2 forms.

I do something wrong when I pass text from form 2 to form 1.

My textbox2 from form 2 doesn't change in my initial form1(are created another form1) when I click on the button, how can I solve it? I want to have only 2 forms, no more.

Form1

Form2

Code:

public partial class Form1 : Form
{
    private string vas;

    public Form1()
    {
        InitializeComponent();
    }

    public string backsend
    {
        get { return vas; }
        set {vas = value; }
    }

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

public partial class Form2 : Form
{
    private string Mn;

    public string passValue
    {
        get { return Mn; }
        set { Mn = value; }
    }

    public Form2()
    {
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        textBox2.Text = Mn;
    }

    private void button2_Click(object sender, EventArgs e)
    {//click for clear textbox1 from form 2.
        textBox2.Clear();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 f1 = new Form1(); i don't understand why is created another form,but not variable
        f1.backsend = textBox2.Text;
        f1.textBox2.Text = f1.backsend; //no exchange in my first form 1
        MessageBox.Show(f1.textBox2.Text);//it's correct
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
user3142035
  • 43
  • 1
  • 8
  • You don't change the `backsend` property anywhere in `Form1` so why do you expect it to change? – BartoszKP Jan 05 '14 at 22:41
  • check this http://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form-in-c-sharp-winforms-application – DayTimeCoder Jan 05 '14 at 22:43
  • sorry i don't understand what you said,please more details... – user3142035 Jan 05 '14 at 22:45
  • You wrote that you do not understand why `var form1 = new Form1()` creates a new form. This question is very basic. I recommend you to read an indrotuctionary book / tutorial on c# – surfmuggle Jan 05 '14 at 22:53
  • i formulate it wrong,i known what it will create new instance,but otherwise i don't know how to do... – user3142035 Jan 05 '14 at 23:07

2 Answers2

1

You've already detected the mistake, the line

Form1 f1 = new Form1(); i don't understand why is created another form,but not variable

This creates a new instance of the class Form1 which you've not shown anywhere, nor do you want to. What you want to do is change the value on the already instantiated and shown form. You can do this by passing the reference to your instantiated Form1 to your f2.

Change this code

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

to

private void button1_Click(object sender, EventArgs e)
{
    Form2 f2 = new Form2();
    f2.initatorForm = this;
    f2.passValue = textBox1.Text;
    f2.Show();
}

and add the appropriate property for initatorForm or you can just use .Parent in your Form2 since the parent should be the form you used to show the form, but I'm not 100% on that.

Also, this way you've done you are only changing the variable vas but not the value of the textbox. You could add that to the setter if you like.

Bikonja
  • 909
  • 5
  • 15
0

I think this example will helped you:

This example will use the string input from Form1 to Form2:

public partial class Form1 : Form
{
    private string vas;

    public Form1()
    {
        InitializeComponent();
    }

    public string Test { get;set; }

    private void button1_Click(object sender, EventArgs e)
    {
        Test = textBox1.Text;
        Form2 f2 = new Form2(this);
        f2.Show();
    }
}

public partial class Form2 : Form
{
    private Form1 form1;

    public Form2(Form1 parentForm)
    {
        InitializeComponent();
        form1 = parentForm;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        //you can use the public string from Form1 here like this:
        textBox1.Text = form1.Test;
    }
}
jomsk1e
  • 3,585
  • 7
  • 34
  • 59