【问题标题】:C# Combobox change selected item while typing in textC# Combobox 在输入文本时更改所选项目
【发布时间】:2011-09-24 06:08:28
【问题描述】:

我有一个组合框。 cmbx 里面有几百件物品。用户必须能够在 cmbx 中输入文本。当用户输入文本时,必须选择以输入值开头的项目。用户必须能够继续输入。

我尝试了下面的代码:

private void cmbGageCode_TextChanged(object sender, EventArgs e)
            {
                int itemsIndex = 0;
                foreach (string item in cmbGageCode.Items)
                {
                    if (item.Contains(cmbGageCode.Text))
                    {
                        cmbGageCode.SelectedIndex = itemsIndex;
                    }
                    itemsIndex++;
                }
            }

这将导致以下结果:当用户在 cmbx 中键入时,包含该值的项目被选中,并且光标位于文本的前面。这意味着每次插入 2 个字符时,都会选择一个项目,而我无法输入完整的值。

有没有人知道如何进行这项工作?也许我需要使用不同的控件?或者也许我正在以完全错误的方式解决这个问题?请帮忙!

【问题讨论】:

  • 您是否尝试过使用内置的自动补全支持?当内置支持完全足够时,没有理由自己编写例程或使用第三方控件。
  • 有一个不错的免费 c# autocomplete control 可用 (with source code) 很容易修改。

标签: c# text combobox


【解决方案1】:

AutoCompleteMode 设置为 SuggestAppendAutoCompleteSource 设置为 ListItems

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.autocompletesource.aspx

【讨论】:

    【解决方案2】:

    试试这个代码。

    private void cmbGageCode_TextChanged(object sender, EventArgs e)
            {
                int itemsIndex = 0;
                foreach (string item in cmbGageCode.Items)
                {
                    if (item.IndexOf(cmbGageCode.Text) == 0)
                    {
                        cmbGageCode.SelectedIndex = itemsIndex;
                        cmbGageCode.Select(cmbGageCode.Text.Length - 1, 0);
                        break;
                    }
                    itemsIndex++;
                }
            }
    

    如果这是你想要的,请告诉我。

    【讨论】:

    • 谢谢。这是我需要的!
    • 太好了,很高兴听到它对您有所帮助。
    • 正如我在评论中提到的,您应该更喜欢使用内置的自动完成支持。确切地说,这段代码有什么不同之处,而您无法立即免费获得?
    • 我不知道是否需要,但是分配 SelectedIndex 会触发自动完成不会触发的“SelectedIndexChanged”事件。也许他正在动态加载表单,其中包含他们选择的信息。这是我能看到的唯一原因。
    【解决方案3】:

    有一个对auto-complete的内置支持,你可以这样做

     ComboBox cmbBox = new ComboBox();
                cmbBox.Items.AddRange(new string[] { "aaa", "bbbb", "dddd"});
                AutoCompleteStringCollection autoCompleteSource= new AutoCompleteStringCollection();
                cmbBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
                foreach (string tempStr in cmbBox.Items)
                    autoCompleteSource.Add(tempStr);
                cmbBox.AutoCompleteCustomSource = autoCompleteSource;
                cmbBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
    
    
                this.Controls.Add(cmbBox);//adding combobox to form control collection
    

    【讨论】:

      【解决方案4】:

      首先,在回答 Cody Gray 时,我需要这个的原因是我的对话框用于不是 STA 的应用程序中,我无法使其成为 STA。自动完成似乎需要 STA。所以,我需要自己做。我对 Skintkingle 的回复做了一些我认为的改进,效果很好。

      private void CB_TextChanged(object sender, EventArgs e)
      {
        try
        {
          CB.TextChanged -= CB_TextChanged;   // Don't respond to text changes from within this function
          int start = CB.SelectionStart;      // Where did user enter new text?
          int length = CB.SelectionLength;    // How much text did they enter?
          if (start > 0) length += start;     // Always consider text from beginning of string
          string text = CB.Text.Substring(0, length); // Look at start of text
          foreach (string item in CB.Items)
          {
            if (item.StartsWith(text, StringComparison.OrdinalIgnoreCase))
            {
              // If the typed text matches one of the items in the list, use that item
              // Highlight the text BEYOND where the user typed, to the end of the string
              // That way, they can keep on typing, replacing text that they have not gotten to yet
              CB.Text = item;
              CB.SelectionStart = length;
              CB.SelectionLength = item.Length - length;
              break;
            }
          }
        }
        finally
        {
          CB.TextChanged += CB_TextChanged;  // Restore ability to respond to text changes
        }
      }
      

      【讨论】:

        猜你喜欢
        • 2011-01-14
        • 1970-01-01
        • 2016-10-02
        • 2014-03-03
        • 2018-07-30
        • 1970-01-01
        • 2015-09-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多