繁体   English   中英

C#-从ComboBox对象属性获取int值

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM