【问题标题】:How can I find out if a combo box contains a value member?如何确定组合框是否包含值成员?
【发布时间】:2012-11-25 14:37:55
【问题描述】:

情况是我有2个控件,一个文本框和一个组合框。用户可以在组合框中选择一些东西,它用值成员填充文本框,如果用户在文本框中输入,我想检查它是否存在于组合框的值中,然后选择相应的显示成员。

我期待的方法是这样的

if(cmb1.valueMembers.Contains(txt1.Text))

但是我找不到这样的东西,我还认为循环通过它们可以找到它?所以我有

foreach (System.Data.DataRowView row in cmb1.Items)
        {}

但在行中的任何位置都找不到值成员?

谢谢

【问题讨论】:

  • 只是一个建议:为什么不使用组合框的 AutoComplete 属性并摆脱文本框? - msdn.microsoft.com/en-us/library/…
  • 另外,看看这篇文章 - stackoverflow.com/questions/3064780/…
  • 我没有使用自动完成,因为值成员是我需要输入到文本框中的内容,显示成员是我在组合框中显示的内容
  • @Dimitar 我已经编辑了我的问题,因为我不太清楚,不过谢谢
  • 如何将值绑定到组合框?也许您可以尝试分配 ComboBox 的 SelectedValue 属性

标签: c# winforms combobox


【解决方案1】:
ListSubCategoryProduct = await Task.Run<List<SubCategoryProduct>>(() => { 
    return productsController.ListSubCategoryProduct(CategoryProductId); }); // <- I search the database

                CboSubCategory.DataSource = ListSubCategoryProduct;
                CboSubCategory.DisplayMember = "Description";
                CboSubCategory.ValueMember = "SubCategoryProductId";

                CboSubCategory.AutoCompleteMode = AutoCompleteMode.Suggest;
                CboSubCategory.AutoCompleteSource = AutoCompleteSource.ListItems;

                this.CboSubCategory.SelectedValue = 1; // <- SubCategoryProductId. You have to know the ID.

【讨论】:

    【解决方案2】:

    游戏有点晚了,但我找不到任何有用的东西,所以我想出了这个简单的解决方案:

    comboBox1.Items.OfType<SomeType>().Any(x => x == YourValue)
    

    或者:

    comboBox1.Items.OfType<SomeType>().Any(x => x.SomeProperty == YourValue)
    

    示例演示:

    class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    
    // ...
    
    var people = new List<Person>() { /* Add some data */ };
    comboBox1.DisplayMember = "Name";
    comboBox1.ValueMember = "Id";
    comboBox1.DataSource = people;
    
    // ...
    
    bool exists = comboBox1.Items.OfType<Person>().Any(p => p.Id == 1);
    

    或者如果你需要获取项目的索引,你可以使用这样的东西:

    var person = comboBox1.Items.OfType<Person>().FirstOrDefault(p => p.Id == 1);
    var index = (person != null) ? comboBox1.Items.IndexOf(person) : -1;
    

    【讨论】:

      【解决方案3】:
      Private Sub ComboBox1_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedValueChanged
          If ComboBox1.SelectedIndex = -1 Then              
              Return
          Else
              TextBox1.Text = ComboBox1.SelectedValue.ToString   ' if find then show their displaymember in combobox.
          End If
      
      
      Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
          Dim value As String = TextBox1.Text
          ComboBox1.SelectedValue = value                                    ' if find then show their displaymember in combobox.
      
          If ComboBox1.SelectedValue Is Nothing Then                          ' if the id you entered in textbox is not find.
              TextBox1.Text = String.Empty
      
          End If
      

      【讨论】:

        【解决方案4】:

        好的,这是一个简单的例子,但我想这是主要思想。我们有一个MyClass,其中Id 用于ValueMember,Name 用于DisplayMember。

         public partial class Form1 : Form
        {
            class MyClass
            {
                public MyClass(string name, int id)
                {
                    Name = name;
                    Id = id;
                }
                public string Name { get; set; }
                public int Id { get; set; }
            }
        
            List<MyClass> dsList = new List<MyClass>();
        
            public Form1()
            {
        
                for (int i = 0; i < 10; i++)
                {
                    dsList.Add(new MyClass("Name" + i , i));
                }
        
                InitializeComponent();
        
                comboBox1.DataSource = dsList;
                comboBox1.ValueMember = "Id";
                comboBox1.DisplayMember = "Name";
            }
        
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                //Checks if item with the typed Id exists in the DataSource
                // and selects it if it's true
                int typedId = Convert.ToInt32(textBox1.Text);
                bool exist = dsList.Exists(obj => obj.Id == typedId);
                if (exist) comboBox1.SelectedValue = typedId;
        
            }
        
        
            private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
            {
                MyClass obj = comboBox1.SelectedValue as MyClass;
                if (obj != null) textBox1.Text = obj.Id.ToString();
            }
        }
        

        如果有不清楚的地方,请随时询问。

        PS:在示例中,我假设将在文本框中输入整数

        【讨论】:

        • 哦,好吧,所以基本上我应该保留一个显示成员和值的列表,以便我可以查看列表来找到关系?我的应用程序工作方式略有不同,但这是完美的,我可以从这里开始,非常感谢!
        • 您总是需要为控件提供数据源。数据源中对象的一个​​属性用于 DisplayMember,另一个用于 ValueMember
        猜你喜欢
        • 2021-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-11-10
        • 1970-01-01
        • 2014-02-14
        • 1970-01-01
        相关资源
        最近更新 更多