【问题标题】:combobox auto complete any string occurrence组合框自动完成任何字符串出现
【发布时间】:2016-02-08 20:15:14
【问题描述】:

我正在使用具有自动完成功能的 ComboBox。正在使用此代码:

comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDown;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBox1.DataSource = items;

DataSource 的items 是List<String>. 假设我有一个姓名列表(如John May),搜索May 将不会显示ComboBox 项目“约翰·梅”。相反,我必须输入“John”才能显示 John May。

我需要进行哪些调整才能搜索整个ComboBox 条目,而不仅仅是字符串的第一部分? 我期待看到一些建议。

【问题讨论】:

    标签: c# winforms combobox autocomplete


    【解决方案1】:

    不幸的是,使用内置功能无法执行类似 contains 的字符串匹配。来自MSDN site(强调我的):

    使用 AutoCompleteCustomSource、AutoCompleteMode 和 AutoCompleteSource 属性创建一个自动组合框 通过将输入的前缀与 维护源中所有字符串的前缀。

    This post 建议通过响应按键事件来手动处理此功能。

    【讨论】:

    • 谢谢。不幸的是,我无法接受。
    【解决方案2】:

    显然,您不能使用 autocomplete 类执行此类操作。相反,您可以使用数据表创建自定义源。数据表允许使用“喜欢”功能搜索单词。

    1. 将数据表创建为主表(在组合框中键入时不要更改此表的数据)。
    2. 将您的列表加载到表格中
    3. 执行搜索操作。
    4. 执行过滤操作并将过滤结果加载到临时表中。
    5. 将临时表设置为组合框的客户来源。

    如需明确答案,请查看MSDN link。

    【讨论】:

      【解决方案3】:

      我无法让第一个 referenced MSDN 使用 Mitja Bonca,而 Pouya Lariyan 的第二个选项对我来说并没有直接起作用(我认为它依赖于 UI 中设置的项目。所以我相信我'm positng 我改编自 Pouya 的代码'

      用法

      autoCompleteCmb = new AutoCompleteCombobox();
      autoCompleteCmb.SelectedIndexChanged += selectedIndexChanged;
      this.Controls.Add(autoCompleteCmb );
      var listTest = new List<string> { "Paul", "Pauline", "Daniel" };
      listTest.ForEach(i=> {autoCompleteCmb.Items.Add(i)});
      

      UserControl AutoCompleteCombobox.cs

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Threading.Tasks;
      using System.Windows.Forms;
      
      namespace MyNamespace
      {
      public partial class AutoCompleteCombobox : ComboBox
      {
          public AutoCompleteCombobox()
          {
              InitializeComponent();
          }
          public IList<object> m_collectionList = null;
      
      
          public AutoCompleteCombobox(IContainer container)
          {
              container.Add(this);
          }
      
          protected override void OnTextUpdate(EventArgs e)
          {
              if (m_collectionList == null)
              {
                  m_collectionList = this.Items.OfType<object>().ToList();
              }
      
              IList<object> values = m_collectionList
                  .Where(x => x.ToString().ToLower().Contains(Text.ToLower()))
                  .ToList<object>();
      
              this.Items.Clear();
              this.Items.AddRange(this.Text != string.Empty ? values.ToArray() : m_collectionList.ToArray());
      
              this.SelectionStart = this.Text.Length;
              this.DroppedDown = true;
              this.Cursor = Cursors.Default;
          }
      
          protected override void OnTextChanged(EventArgs e)
          {
              if (this.Text != string.Empty) return;
              this.Items.Clear();
              this.Items.AddRange(m_collectionList.ToArray());
          }
      
        }
      }
      

      【讨论】:

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