2013-10-16 134 views
-1

我想填充ComboBox一些字符串像獲得價值

comboBox1.DataSource = new List<string> { "By title", "by isbn", "by tag"}; 

我如何可以利用選擇從這個ComboBox,因爲我不知道該怎麼把爲

comboBox1.DisplayMembercomboBox1.ValueMember

+0

在此回答:http://stackoverflow.com/questions/1610695/c-sharp-binding-generic-liststring-to-combo-box –

回答

1

只需使用SelectedItem物業:

var selectedItem = (comboBox1.SelectedItem ?? "").ToString(); 
0

在你的情況,你的產品只是一個簡單的字符串,因此,文本=項。但是如果你有更復雜的結構,你可能需要一個這種類型的數組,並設置DisplayMemberValueMember

然後,你的代碼應該是這樣的:

myType mt = (mytype)comboBox1.SelectedItem; 
// Do something based on other type properties