C# 如何以另一种形式显示标签中文本框的值?

C# 如何以另一种形式显示标签中文本框的值?,c#,forms,winforms,C#,Forms,Winforms,嗨,我是C#的新手,我想在form1中显示textBox1的值,在form2中显示Label1的值。我试着用这个: private void button1_Click(object sender, EventArgs e) { label1.Text = textBox1.Text; Form2 frm = new Form2(); frm.Show(); this.Hide(); } 但它不起作用,因为它是另

嗨,我是C#的新手,我想在form1中显示textBox1的值,在form2中显示Label1的值。我试着用这个:

private void button1_Click(object sender, EventArgs e)
    {
        label1.Text = textBox1.Text;

        Form2 frm = new Form2();
        frm.Show();
        this.Hide();
    }

但它不起作用,因为它是另一种形式。有人能告诉我怎么做吗?

在form1中编写以下代码

private void button1_Click(object sender, EventArgs e)
{
        Form2 form = new Form2(TextBox1.Text);
        form.Show();
    }
在表格2中写下这个

public Form2(string text)
    {
        InitializeComponent();
        label1.Text = text;
    }
尝试这样做:

 // In Form1.cs.
private Form2 otherForm = new Form2();
private void GetOtherFormTextBox()
{
    textBox1.Text = otherForm.label1.Text;
}
private void button1_Click(object sender, EventArgs e)

    GetOtherFormTextBox();
}
另一种选择

表格一

private void button1_Click(object sender, EventArgs e)
{
    Form2 frm = new Form2();
    frm.LabelText = "My Text";
    from.ShowDialog();
}
表格二

private string labelText;
public string LabelText { get { return labelText; } set { labelText = value; } }
private void Form2_Load(object sender, EventArgs e)
{
    label.Text = LabelText;
}

你有几种方法可以做到这一点。首先,您可以将
Form2
的构造函数更改为获取字符串。创建
Form2
时,将其传递给
textBox1.Text
,然后在构造函数中为标签赋值。另一种方法是在
Form1
上设置一个
public
值,该值只返回
textBox1.Text
,然后从
Form2
读取,但在我看来这有点复杂,也不太好用。只需谷歌“如何在表单之间传递值”,由于这个原因,这很可能会作为一个副本关闭。您是否考虑过在form2自动生成并调用构造函数中创建一个公共静态类方法