简体   繁体   中英

Sending variables from Form2 to Form1

I wanna know how I can send variables from Form2 to Form1. I have one textbox and button in Form1 and one textbox and button in Form2. My application starts at Form1, textbox1 is empty and by clicking button Form2 will appear. In Form2 I want to write number and by clicking on the button send it to Form1 textbox.

I was trying this code, but I dont know how to solve it.

Form1 code:

public static int number;
public Form1()
{
    InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
    Form2 form = new Form2();
    form.Show();
}

Form2 code

public Form2()
{
    InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
    Form1.number = textBox1.Text;
    this.Visible = false;
}

Now I have variable called number in Form1, which contains value of Form2 Textbox, right? But how do I say: textbox1.text(Form1) = number after that action? Do I need refresh Form1 somehow?

Thanks!

I'd say a nice easy way to do this kind of thing, is via making a public event:

In form two, add an event:

public partial Class Form2
{
    public event Action<string> SomethingHappened;
...

We need to fire the event on Form2 - to notify subscribers:

//On Form2
private void button1_Click(object sender, EventArgs e)
{
    if(SomethingHappened != null)
        SomethingHappened (textBox1.Text);
}

Then, upon creation 'subscribe' the parent form Form1 to action on the sub-form:

Form2 form = new Form2();

//Here, we assign an event handler
form.SomethingHappened += (string valueFromForm2) => 
{
    //Event handled on Form1
    this.Number = valueFromForm2;
};

The setup sounds kinda like a settings dialog where you can't continue in Form1 until Form2 is closed.

If this is the case, then something more like his would be appropriate in Form1:

public partial class Form1 : Form
{

    private int number = 411;

    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        this.textBox1.Enabled = false;
        this.textBox1.Text = number.ToString();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2(this.number);
        if (f2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            this.number = f2.Number;
            this.textBox1.Text = this.number.ToString();
        }
    }

}

With Form2 looking something like:

public partial class Form2 : Form
{

    public Form2(int number)
    {
        InitializeComponent();
        this.textBox1.Text = number.ToString();
    }

    private int number = 0;
    public int Number
    {
        get { return this.number; }
    }

    private void btnOK_Click(object sender, EventArgs e)
    {
        int value;
        if (int.TryParse(this.textBox1.Text, out value))
        {
            this.number = value;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        else
        { 
            MessageBox.Show(textBox1.Text, "Invalid Integer");
        }
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM