C#-从ComboBox对象属性获取int值
我正在使用Windows窗体。是否有方法从当前在C#-从ComboBox对象属性获取int值,c#,object,properties,combobox,C#,Object,Properties,Combobox,我正在使用Windows窗体。是否有方法从当前在comboBox1中选择的对象获取新对象.number\u prop值?最好不使用组合框1索引。任何帮助都将不胜感激。我一直在寻找解决办法 sampleObject new_object = new sampleObject(); new_object.text_prop = "sample text"; new_object.number_prop = 3; comboBox1.Items.Insert(0, new_object); cla
comboBox1
中选择的对象获取新对象.number\u prop
值?最好不使用组合框1
索引。任何帮助都将不胜感激。我一直在寻找解决办法
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;
}
}
将combobox的valueitem设置为“number\u prop”,将displayitem设置为“text\u prop”您可能正在谈论以下内容:
var selectedObject = (sampleObject) comboBox1.SelectedItem;
var value = selectedObject.number_prop;
另外请注意,
object
是C#中的保留字(作为object
class的别名)。您的代码应该是这样的
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;
}
}
对非常感谢你!“这确实解决了问题。”格兰特·温尼在我的例子中编辑了这个类。很抱歉。