从 form2 中的 form1 访问文本框值 [英] Accessing a textbox value from form1 in form2

查看:26
本文介绍了从 form2 中的 form1 访问文本框值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 form1 上有一个文本框.
我想要做的是将文本框的值从form1获取到form2中.
我该怎么做?

I have a textbox on form1.
What I want to do is to get the value of the textbox from form1 into form2.
How can I do this?

推荐答案

我所做的是创建一个新项目并添加第二个表单,然后在两个表单中添加一个文本框,在 Form1 上有一个按钮来推送其文本的值框到 Form2.

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.

要实现此目的,请在 Form2 上创建一个属性并从 Form1 设置它.像这样:

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

通过这种方式,如果需要,也可以使用相同的属性从 Form2 中提取数据.

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

这篇关于从 form2 中的 form1 访问文本框值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆