【问题标题】:getting value from combobox selection从组合框选择中获取价值
【发布时间】:2013-10-25 03:48:41
【问题描述】:

我想用一些字符串填充ComboBox

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

由于我不知道该放什么,我该如何利用 ComboBox 中的选择

comboBox1.DisplayMembercomboBox1.ValueMember?

【问题讨论】:

标签: c# .net winforms


【解决方案1】:

只需使用SelectedItem 属性:

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

【讨论】:

    【解决方案2】:

    在您的情况下,您的项目只是一个简单的字符串,因此 text = item。但如果您有更复杂的结构,您可能需要该类型的数组并设置DisplayMemberValueMember

    然后,您的代码应该如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多