簡體   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