I'm using Windows Forms. Is there way to get new_object.number_prop
value from the object currently selected in comboBox1
? Preferably without using the comboBox1
indexes. Any help would be greatly appreciated. I've been looking for a solution for a while now.
sampleObject new_object = new sampleObject();
new_object.text_prop = "sample text";
new_object.number_prop = 3;
comboBox1.Items.Insert(0, new_object);
class sampleObject
{
public string text_prop {get; set; }
public int number_prop {get; set; }
public override string ToString();
{
return text_prop;
}
}
Probably you are talking about this:
var selectedObject = (sampleObject) comboBox1.SelectedItem;
var value = selectedObject.number_prop;
Also please note, that object
is reserved word (as alias to Object
class) in C#.
Your code should be like this.
object new_object = new object();
new_object.text_prop = "sample text";
new_object.number_prop = 3;
comboBox1.Items.Insert(0, new_object);
comboBox1.ValueMember = "number_prop";
comboBox1.DisplayMember = "text_prop"
class SomeObject
{
public string text_prop {get; set; }
public int number_prop {get; set; }
public override string ToString();
{
return text_prop;
}
}
将组合框的valueitem设置为“ number_prop”并将displayitem设置为“ text_prop”
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.