C# 如何设置文本框控件的设置和获取?

C# 如何设置文本框控件的设置和获取?,c#,controls,C#,Controls,我想将Form1中TextBox的值获取给另一个类 我试图创建一个集合并获取,但我不能这样做,因为VS显示了代码中关于歧义的错误 public partial class Form1 : Form { private TextBox _textBox1; public Form1() { this._textBox1 = textBox1; InitializeComponent(); } public string _

我想将
Form1
TextBox
的值获取给另一个类

我试图创建一个集合并获取,但我不能这样做,因为VS显示了代码中关于歧义的错误

public partial class Form1 : Form
{
    private TextBox _textBox1;

    public Form1()
    {
        this._textBox1 = textBox1;
        InitializeComponent();
    }

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

如何使之正确?我的控件是私有的。

您可以将
textBox1.Text
传递给变量,并为其生成getter/setter

像这样:

public class A : Form1
{
    // assuming it's a string. If it's not, change the type
    // for the getter method below accordingly
    private string textBoxValue;

    // at some point, you'll have to make this line below:
    textBoxValue = textBox1.Value;

    public string GetTextBoxValue()
    {
        return textBoxValue;
    }
}

public class B 
{
    A aReference = new A();

   // you can get the value you want by doing
   // aReference.GetTextBoxValue();
}

您可以将
textBox1.Text
传递给变量,并为其生成getter/setter

像这样:

public class A : Form1
{
    // assuming it's a string. If it's not, change the type
    // for the getter method below accordingly
    private string textBoxValue;

    // at some point, you'll have to make this line below:
    textBoxValue = textBox1.Value;

    public string GetTextBoxValue()
    {
        return textBoxValue;
    }
}

public class B 
{
    A aReference = new A();

   // you can get the value you want by doing
   // aReference.GetTextBoxValue();
}

如果类中有一个字段和一个属性具有相同的名称,请将该属性的名称更改为

    public string FormTextBox1
    {
        get { return _textBox1.Text; }
        set { _textBox1.Text = value; }
    }
作为命名标准,公共属性必须是Pascal大小写符号


如果类中有一个字段和一个属性具有相同的名称,请将该属性的名称更改为

    public string FormTextBox1
    {
        get { return _textBox1.Text; }
        set { _textBox1.Text = value; }
    }
作为命名标准,公共属性必须是Pascal大小写符号


您有两个名为
\u textbox1
的属性,一个是字符串,另一个是文本框。@Scrobi一个字段和一个属性是完全相同的。。您想要什么?要访问私有
TextBox
或访问此
TextBox
Text
属性,请清除
private TextBox\u textBox1
只需在string属性中使用
textBox1.Text
。您有两个名为
\u textBox1
的属性,一个是字符串,另一个是文本框。@Scrobi一个字段和一个属性完全相同。。您想要什么?要访问私有
TextBox
或访问此
TextBox
Text
属性,请清除
private TextBox\u textBox1
只需在string属性中使用
textBox1.Text