简体   繁体   中英

C# Combobox change selected item while typing in text

I've got an combobox. The cmbx had a few hundred items in it. The user must be able to type in text into the cmbx. While the user is typing in the text, the item that starts with the typed value must be selected. The user must be able to continue typing.

I tried the code below:

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

This results in the folowing: When a user types in the cmbx the item that contains the value is selected, and the cursor is placed at the front of the text. This means that when ever 2 characters are inserted, a item is selected and I'm unable to type in the complete value.

Does anyone have an idea on how to make this work? Maybe I need to use a different control? Or maybe I'm going about this in completely the wrong way? Please help!

With AutoCompleteMode set to SuggestAppend and AutoCompleteSource set to ListItems

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

Try this code.

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

Let me know if this is what you want.

There is a built in support for auto-complete , you can do

 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

First, in answer to Cody Gray, the reason I needed this was that my dialog is used in an app that is not STA and I cannot make it STA. AutoComplete seems to require STA. So, I needed to do it myself. I've made what I think are some improvements to Skintkingle's reply and it works great.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM