Transferring data from Form2 (textbox2) to Form1 (

2020-03-26 07:56发布

Possible Duplicate:
I would like to control Form1 from Form2

I'm a newbie to C# and I can't find the answer I'm looking for in google, so I'm hoping someone here could help me. I'm only practicing to transfer data (or pass, call it however you want) from a form to another.

Here's what I have:

I have 2 forms - Form1 and Form2.
Form1 contains a textbox (named txtForm1) and a button (named btnForm1).
Form2 contains a textbox (named txtForm2) and a button (named btnForm2).

After running the application, by clicking the button btnForm1, the user opens Form2. The text that the user writes in the textbox (txtForm2) should be transfered to the textbox (txtForm1, which button is disabled) in Form1.

How can I do this transfer?

Edited:
Okay i need to be clear that this is all the code i have:

Form1 (button which opens Form2):

    private void btnForm1_Click(object sender, EventArgs e)
    {
        new Form2().Show();
    }

Form2 (button which closes Form2):

    private void btnForm2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

I have NOTHING ELSE. (I'm a total newbie)

标签: c# winforms
4条回答
beautiful°
2楼-- · 2020-03-26 08:29

Make a public variable and pass it the value from your text box and then onto your second form.

public static string myVar;   
myVar = txtForm2.Text;

and when you return to the first form: txtForm1.Text = Form2.myVar;

查看更多
家丑人穷心不美
3楼-- · 2020-03-26 08:36

In your Form2 you should have some like:

private void btnForm2_Click(object sender, EventArgs e)
 {          
   this.Hide();       
 }


public String GettxtForm2()
{
    return txtForm2.Text;
}

Now in form1 you can acces that txtForm2 with something like:

Form2 form2 = new Form2();
 //on click btnForm1 show that form2 where you can edit the txtForm2
 private void btnForm1_Click(object sender, EventArgs e)
     {                
       form2.Show();       
     }
   //after you save the txtForm2 when you will focus back to form1 the txtForm1 will get the value from txtForm2
   private void Form1_Enter(object sender, EventArgs e)
        {
             txtForm1.Text = Form2.GettxtForm2();
        }

You can easy modify the events where all this logic can occur...

查看更多
做个烂人
4楼-- · 2020-03-26 08:37

in Form1:

public void SetTextboxText(String text)
{
    txtForm1.Text = text;
}

private void btnForm1_Click(object sender, EventArgs e)
{
    var frm = new Form2(this); // pass parent form (this) in constructor
    frm.Show();
}

in Form2:

Form _parentForm;

public Form2(Form form)
{
    _parentForm = form;
}

private void txtForm2_TextChanged(object sender, EventArgs e)
{
    _parentForm.SetTextboxText(txtForm2.Text); // change Form1.txtForm1.Text
}
查看更多
唯我独甜
5楼-- · 2020-03-26 08:43

Try this ;)

On Form1:

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

On form2:

public partial class Form2 : Form
{
    public string textBoxValue;
    public Form2()
    {
        InitializeComponent();
    }

    public Form2(string textBoxValue)
    {
        InitializeComponent();
        this.textBoxValue = textBoxValue;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        textBox2.Text = textBoxValue;
    }
查看更多
登录 后发表回答