0

我在 form1 上有一个文本框。
我想要做的是将文本框的值从 form1 获取到 form2 中。
我怎样才能做到这一点?

4

2 回答 2

1

What I did was create a new project and add a second form then added a textbox to both forms, with a button on Form1 to push the value of its text box to Form2.

To achieve this, create a Property on Form2 and set it from Form1. Like this:

Form1

public partial class Form1 : Form
{
    Form2 frm2;
    public Form1()
    {
        InitializeComponent();
        frm2 = new Form2();
        frm2.Show(this);
    }

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

Form2

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

    public string ModifyTextBoxValue
    {
        get { return textBox1.Text; }
        set { textBox1.Text = value; }
    }
}

Done this way, the same property can also be used to pull data from Form2 if desired.

于 2012-11-26T01:58:30.933 回答
0

您可以使用 .Tag 属性(在这里查看我的问题 ,简单的方法是这样的:在 form2 中添加另一个文本框

在表格 1 中执行此操作。此代码将 texBox.text 存储在 form1

try
{
    private void change_Click(object sender, EventArgs e)
    {
         form1 frm1 = new form();
         frm1.Tag = this.textBox1.text;
         frm1.ShowDialog();
    }
}
catch (Exception ex)
{
   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

然后在加载 form2 时写下这个。此代码将 texBox2 的值替换为 texBox1 的值

string myText = (string)this.Tag;
   this.textBox2.text = myText;
于 2012-11-26T01:38:48.727 回答