簡體   English   中英

C# Combobox 在輸入文本時更改所選項目

[英]C# Combobox change selected item while typing in text

我有一個 combobox。 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 中鍵入時,包含該值的項目被選中,並且 cursor 被放置在文本的前面。 這意味着每次插入 2 個字符時,都會選擇一個項目,而我無法輸入完整的值。

有沒有人知道如何進行這項工作? 也許我需要使用不同的控件? 或者,也許我正在以完全錯誤的方式解決這個問題? 請幫忙!

AutoCompleteMode設置為SuggestAppend並將AutoCompleteSource設置為ListItems

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

試試這個代碼。

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++;
            }
        }

讓我知道這是否是你想要的。

有一個對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

首先,在回答 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
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM