【问题标题】:Can't get Value from ComboBox无法从 ComboBox 获取值
【发布时间】:2009-12-05 02:39:15
【问题描述】:

我有一个简单的组合框,其中包含一些值/文本项。我使用 ComboBox.DisplayMember 和 ComboBox.ValueMember 来正确设置值/文本。当我尝试获取该值时,它返回一个空字符串。这是我的代码:

FormLoad 事件:

cbPlayer1.ValueMember = "Value";
cbPlayer1.DisplayMember = "Text";

ComboBox 事件的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?)

我做错了什么?

【问题讨论】:

    标签: c# .net combobox


    【解决方案1】:

    不确定 ComboBox.SelectedValue 是什么意思,它有一个 SelectedItem 属性。添加项目时不会设置,只有在用户进行选择时才会设置。

    Items 属性是 System.Object 的集合。这允许组合框存储和显示任何类型的类对象。但是您必须将其从对象转换为您的类类型才能在代码中使用选定的对象。这在您的情况下不起作用,您添加了一个匿名类型的对象。您需要声明一个小的帮助类来存储 Value 和 Text 属性。一些示例代码:

      public partial class Form1 : Form {
        public Form1() {
          InitializeComponent();
          comboBox1.Items.Add(new Item(1, "one"));
          comboBox1.Items.Add(new Item(2, "two"));
          comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
        }
        void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
          Item item = comboBox1.Items[comboBox1.SelectedIndex] as Item;
          MessageBox.Show(item.Value.ToString());
        }
        private class Item {
          public Item(int value, string text) { Value = value; Text = text; }
          public int Value { get; set; }
          public string Text { get; set; }
          public override string ToString() { return Text; }
        }
      }
    

    【讨论】:

    • 这是我更喜欢的方法。感谢您的帮助,它成功了。
    【解决方案2】:

    正如您在调试器中看到的,SelectedItem 包含您需要的信息。但是要访问 SelectedItem.Value,您需要将 SelectedItem 强制转换为适当的类型(如果您使用匿名类型,这会出现问题)或使用反射。 (VS 无法编译 SelectedItem.Value,因为在编译时 VS 只知道 SelectedItem 是 Object 类型,它没有 Value 属性。)

    要使用反射来获取 Value 成员,请将 Type.InvokeMember 与 BindingFlags.GetProperty 结合使用。

    要转换 SelectedItem,请声明一个具有 Value 和 Text 属性的命名类型,而不是使用匿名类型,并将命名类型的实例添加到 ComboBox,而不是匿名类型的实例。然后投射 SelectedItem: ((MyType)(cb.SelectedItem)).Value.

    【讨论】:

      【解决方案3】:

      不知道为什么SelectedValue 没有返回任何东西...我认为这可能是因为您没有使用数据绑定 (DataSource)。您应该尝试将卡片列表分配给 DataSource 属性。

      关于SelectedItem 的问题:ComboBox.SelectedItem 的类型为Object,它没有名为Value 的属性。您需要将其转换为项目的类型;但由于它是匿名类型,你不能......你应该创建一个类型来保存卡片的值和文本,并转换为这种类型:

      Card card = ComboBox.SelectedItem as Card;
      if (card != null)
      {
          // do something with card.Value
      }
      

      【讨论】:

        【解决方案4】:

        您正在修改 SelectedIndexChanged 处理程序中 ComboBox 的内容。当您修改内容时,会导致所选项目被取消设置。设置你正在阅读为null,在消息框中显示为空字符串。

        【讨论】:

          【解决方案5】:

          我很好奇您是将组合框绑定到集合还是手动填充它。如果您将组合框绑定到某种数据源......您应该将项目添加到数据源,而不是组合框本身。将项目添加到数据源时,组合框应以实物形式更新。

          如果您没有绑定,则添加项目不会导致该项目被选中。您需要等待用户选择项目,或者以编程方式在代码中选择项目。

          【讨论】:

            【解决方案6】:

            为避免必须为所有组合框创建新类,我建议您只使用 KeyValuePair,如下例所示:

            cbPlayer1.ValueMember = "Value";
            cbPlayer1.DisplayMember = "Key";
            
            cbPlayer1.DataSource = new List<KeyValuePair<string,string>>()
            {new KeyValuePair<string,string>("3","This should have the value of 3")};
            

            你仍然需要转换选择的值

            string selectedValue = (string)cbPlayer1.SelectedValue;
            
            MessageBox.Show(selectedValue);
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-09-09
              • 2011-10-13
              • 2013-09-26
              • 2021-06-14
              • 1970-01-01
              相关资源
              最近更新 更多