简体   繁体   English

C# 从不同的表单调用文本框

[英]C# calling on a textbox from a different form

I am trying to access the textbox of form1 (Econtact) from form2.我正在尝试从 form2 访问 form1 (Econtact) 的文本框。 I made the textbox public but it didn't help.我公开了文本框,但没有帮助。

This is the code in form2这是form2中的代码

    TextBox Form2txtboxFirstName = Econtact.txtboxFirstName;

the following code is giving me the error An object reference is required for the non-static field, method, or property 'Econtact.txtboxFirstName'以下代码给了我错误非静态字段、方法或属性“Econtact.txtboxFirstName”需要 object 引用

If Econtact form is child of Form2, then pass reference of Form2如果 Econtact 表单是 Form2 的子表单,则传递 Form2 的引用

public partial class Form2 : Form
{
    private EContact _eContact;

    public Form2(EContact eContact)
    {
        InitializeComponent();

        _eContact = eContact;
        MsgBox.Show(_eContact.txtboxFirstName.Text);
    }       
}

inside EContact form however, you will have to pass the reference while calling the form2但是,在 EContact 表单中,您必须在调用 form2 时传递引用

var form2 = New Form2(this);
form2.Show();

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

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