[英]C# - Getting int value from ComboBox object property
我正在使用Windows窗体。 有没有办法从comboBox1
当前选择的对象中获取new_object.number_prop
值? 最好不使用comboBox1
索引。 任何帮助将不胜感激。 我一直在寻找解决方案已有一段时间了。
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;
}
}
可能您在谈论这个:
var selectedObject = (sampleObject) comboBox1.SelectedItem;
var value = selectedObject.number_prop;
还请注意,该object
是C#中的保留字(作为Object
类的别名)。
您的代码应如下所示。
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.