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.
3 answers
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.
Valuein a text-box can make some sense if it is the heir of some basic control, and will be of typeobject, and in the case of a text-box it should store the same value as theTextproperty - Artur Udod