c# - Can't get Value from ComboBox -
I have a simple combo box with some value / text item in it. I am using a combo box. Displaymilm and combo box. Valuemail, set true / value when I try to get value, it returns an empty string. Here is my code:
Formload Event:
cbPlayer1.ValueMember = "value"; CbPlayer1.DisplayMember = "Text"; Select the combo box eventindex: cbPlayer1.Items.Add (new {value = "3", text = "should have value of 3"}); MessageBox.Show (cbPlayer1.SelectedValue + "");
and it returns a blank dialog box. I had also selected the combo box. The selected item.Value (see VS, see picture) but it does not compile:
'Object' has no definition for 'value' and no extension method 'value The first argument of type 'object' can be accepted (do not you remember the use director or assembly reference?)
What am I doing?
Not sure what the combo box. The selected value means it has a selected item property, it is not set when you add an item, only when the user selects one.
The item properties are a collection of systems. Object allows combo box to store and display any kind of class object. But you have to insert your class type from the object to use the selected object in your code. He can not work in your case, you have added an object of an unknown type. You will need to declare a small assistant class to store values and text properties. Some sample code:
public partial class form 1: form {public form 1 () {initial group (); ComboBox1.Items.Add (new item (1, "one")); ComboBox1.Items.Add (new item (2, "two")); Combo box 1. Selected indices changed + = new event handler (combo box 1 select index change); } Zero comboBox1 selected from the index (Object Sender, EventArgs E) {item item = comboBox1.Items [comboBox1.SelectedIndex] as the item; MessageBox.Show (item.Value.ToString ()); } Private class items {public items (integer value, string text) {value = value; Text = text; } Public int value {get; Set; } Public string text {get; Set; } Public override string toasting () {return text; }}}
Comments
Post a Comment