[英]C# - Getting int value from ComboBox object property
I'm using Windows Forms. 我正在使用Windows窗体。 Is there way to get new_object.number_prop
value from the object currently selected in comboBox1
? 有没有办法从comboBox1
当前选择的对象中获取new_object.number_prop
值? Preferably without using the comboBox1
indexes. 最好不使用comboBox1
索引。 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#. 还请注意,该object
是C#中的保留字(作为Object
类的别名)。
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”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.