I have a car code and its name. What I need to use is to write them both into a form. ( textbox.text="Название авто", textbox.value=код-авто ) textbox.text="Название авто", textbox.value=код-авто did not find this in the textbox-e.

  • one
    TextBox is designed to display text, not to store any data. But if you really need much, you can use the Tag property which is used to store the object reference. Or if these are properties of a class, then use the DataBindings binding. PS according to the rules of registration, greetings and thanks are not indicated in the question, tags are used to list technologies. - Alex Krass
  • What should be the result? TextBox with two visible values ​​for editing or TextBox with a name like in Delphi? and you can guess for a long time - rdorn
  • Value in a text-box can make some sense if it is the heir of some basic control, and will be of type object , and in the case of a text-box it should store the same value as the Text property - Artur Udod

3 answers 3

You can use your component, which is a successor from TextBox. For example, for the class

 public class myAuto { public string Name { get; set; } public object Kod { get; set; } public override string ToString() { return Name; } } 

such an heir would be fine:

 public class myTextBox : TextBox { object _value; public object Value { get { return _value; } set { _value = value; Text = string.Format("{0}", value); } } } 

Assign and take the value you need through the Value:

 // после этого текст myTextBox1 будет "Название авто" myTextBox1.Value = new myAuto(){ Name = "Название авто", Kod = "KOD" } // получаем экземпляр класса myAuto myAuto _myAuto = (myAuto)myTextBox1.Value; 

    In WinForms, almost any control has a Tag property in which you can put data invisible to the user:

     textBox1.Text = "Название авто"; textBox1.Tag = 42; // код авто 

    and later to get them from there, leading to the type you need:

     var code = (int)textBox1.Tag; 

      You need to separate content from presentation. Your content is a class (let's call it Car ), containing the car code, its name and all other necessary data. You use a textbox (or several textboxes) to display data. But you do not have to take data back from textboxes (only if the user edits them), take data from the content - Car class.