Как установить и получить для элемента управления Textbox? - PullRequest
0 голосов
/ 21 декабря 2018

Я хочу получить значение TextBox в Form1 для другого класса.

Я пытаюсь сделать набор и получить, но я не могу этого сделать, потому что 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; }
    }
}

Как это исправить?Мой контроль является личным.

Ответы [ 3 ]

0 голосов
/ 21 декабря 2018

В вашем классе есть одно поле и одно свойство с тем же именем, измените имя свойства, например, на

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

в качестве стандарта именования, общедоступные свойства должны быть обозначением Pascal Case

Соглашения о капитализации

0 голосов
/ 21 декабря 2018
    public void yourFormLoadMethod()
    {
        //this instantiates a new object of your class
        nameOfYourClass newYourObject = new nameOfYourClass(//put any params you need here);

        txtNameOfYourTextBox.DataBindings.Add("Enabled", newLTDObjectBenInt, "YourTextBoxEnabled", true, DataSourceUpdateMode.OnPropertyChanged);
        txtNameOfYourTextBox.DataBindings.Add("Value", newLTDObjectBenInt, "YourTextBoxEntered", true, DataSourceUpdateMode.OnPropertyChanged);
        txtNameOfYourTextBox.DataBindings.Add("Visible", newLTDObjectBenInt, "YourTextBoxVisible", true, DataSourceUpdateMode.OnPropertyChanged);
    }

    public class nameOfYourClass
    {

        //constructor
        public nameOfYourClass(//same params here from the Load method)
        {
            //place any logic that you need here to load your class properly

            //this sets default values for Enable, Visible and the text
            //you use these fields to manipulate your field as you wish
            yourTextBoxVisible = true;
            yourTextBoxEnabled = true;
            yourTextBoxEntered = "this is the default text in my textbox";
        }

        private bool yourTextBoxEnabled;
        public bool YourTextBoxEnabled
        {
            get
            {
                return yourTextBoxEnabled;
            }
            set
            {
                yourTextBoxEnabled = value;
            }
        }
        private bool yourTextBoxVisible;
        public bool YourTextBoxVisible
        {
            get
            {
                return yourTextBoxVisible;
            }
            set
            {
                yourTextBoxVisible = value;
            }
        }
        private string yourTextBoxEntered;
        public string YourTextBoxEntered
        {
            get
            {
                return yourTextBoxEntered;
            }
            set
            {
                yourTextBoxEntered = value;
            }
        }
    }
0 голосов
/ 21 декабря 2018

Вы можете передать textBox1.Text переменной и создать для нее метод получения / установки.

Примерно так:

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();
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...