简体   繁体   English

将变量从Form2发送到Form1

[英]Sending variables from Form2 to Form1

I wanna know how I can send variables from Form2 to Form1. 我想知道如何将变量从Form2发送到Form1。 I have one textbox and button in Form1 and one textbox and button in Form2. 我在Form1中有一个文本框和按钮,在Form2中有一个文本框和按钮。 My application starts at Form1, textbox1 is empty and by clicking button Form2 will appear. 我的应用程序从Form1开始,textbox1为空,单击Form2按钮将出现。 In Form2 I want to write number and by clicking on the button send it to Form1 textbox. 在Form2中,我想输入数字,然后单击按钮将其发送到Form1文本框。

I was trying this code, but I dont know how to solve it. 我正在尝试此代码,但我不知道如何解决它。

Form1 code: Form1代码:

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

Form2 code Form2代码

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? 现在我在Form1中有一个名为number的变量,其中包含Form2 Textbox的值,对吗? But how do I say: textbox1.text(Form1) = number after that action? 但是我怎么说:textbox1.text(Form1)=该操作之后的数字? Do I need refresh Form1 somehow? 我是否需要以某种方式刷新Form1?

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: 我们需要在Form2上触发该事件-通知订阅者:

//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: 然后,在创建后,“订阅”父表单Form1以对子表单进行操作:

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. 该设置听起来有点像设置对话框,在该对话框中,直到关闭Form2后才能在Form1中继续。

If this is the case, then something more like his would be appropriate in Form1: 如果是这种情况,那么在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: 与Form2看起来像:

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");
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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