C#Combobox在输入文本时更改所选项目

时间:2011-06-28 11:45:13

标签: c# 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时,选择包含该值的项目,并将光标放在文本的前面。这意味着当插入2个字符时,会选择一个项目,我无法输入完整的值。

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

4 个答案:

答案 0 :(得分:4)

AutoCompleteMode设置为SuggestAppend并将AutoCompleteSource设置为ListItems

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

答案 1 :(得分:3)

试试这段代码。

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

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

答案 2 :(得分:1)

内置支持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

答案 3 :(得分:0)

首先,为回答Cody Gray,我需要这样做的原因是我的对话框用于非STA应用程序中,而我无法使其成为STA。 AutoComplete似乎需要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
  }
}