我有一個簡單的組合框,其中包含一些價值/文本項目。我使用ComboBox.DisplayMember和ComboBox.ValueMember正確設置值/文本。當我嘗試獲取值時,它會返回一個空字符串。這裏是我的代碼:無法從組合框中獲取價值
FormLoad事件:
cbPlayer1.ValueMember = "Value";
cbPlayer1.DisplayMember = "Text";
SelectIndexChanged組合框的事件:
cbPlayer1.Items.Add(new { Value = "3", Text = "This should have a value of 3" });
MessageBox.Show(cbPlayer1.SelectedValue+"");
,並返回一個空的對話框。我也試過ComboBox.SelectedItem.Value(其中VS看到,見圖片),但它不會編譯:
'object' does not contain a definition for 'Value' and no extension method 'Value' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
我在做什麼錯?
這是我更喜歡的方法。感謝您的幫助,它的工作。 – ademers 2009-12-05 03:20:44