Items in the ComboBox control are not selected while you type the first characters

Symptoms

You set the DropDownStyle property of the ComboBox control to DropDownList, and then type the first characters of the item in the ComboBox during run time. The item that is selected in the ComboBox does not match the characters that you type. The item that is selected in the ComboBox is based on the last character that you type. For Example, if the items in the ComboBox are red, oak, and rose, when you type ro in the ComboBox, the focus is on oak instead of rose.

Cause

This problem occurs because the ComboBox search is based on one character instead of the complete character set.

Workaround

To work around this problem, add code that searches for items with all the characters that you type in the ComboBox. The code must also select the item that closely matches all the characters. To clear the search text at regular intervals, use a Timer control. To do this, follow these steps:
  1. In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, create a new Windows application by using Visual Basic .NET, Visual Basic 2005, or Visual C# .NET.

    By default, Form1 is created.
  2. From the toolbox, drag a Timer to Form1.
  3. In the Properties window, set the Enabledproperty to True.
  4. Add the following statement to the variable declaration section of the code:

    Visual Basic .NET or Visual Basic 2005
    Dim searchstr As String
    Visual C# .NET
    string searchstr;
  5. Add the following code to the Load event of Form1:

    Visual Basic .NET or Visual Basic 2005
    'set the timer interval and start the timer
    Timer1.Interval = 1000
    Timer1.Start()
    Visual C# .NET
    //set the interval  and start the timer
    timer1.Interval =1000;
    timer1.Start();
  6. Add the following code to the Tick event of Timer1:

    Visual Basic .NET or Visual Basic 2005
    'empty the string for every 1 seconds
    searchstr = ""
    Visual C# .NET
    //empty the string for every 1 seconds
    searchstr="";
  7. Add the following code to the KeyUp event of ComboBox1:

    Visual Basic .NET or Visual Basic 2005
          searchstr = searchstr & Chr(e.KeyValue)
    ' If the Search string is greater than 1 then use custom logic
    If searchstr.Length > 1 Then
    Dim index As Integer
    ' Search the Item that matches the string typed
    index = ComboBox1.FindString(searchstr)
    ' Select the Item in the Combo
    ComboBox1.SelectedIndex = index
    End If
    Visual C# .NET
             searchstr = searchstr + Convert.ToChar(e.KeyCode);
    // If the Search string is greater than 1 then use custom logic
    if (searchstr.Length > 1)
    {
    int index;
    // Search the Item that matches the string typed
    index=comboBox1.FindString(searchstr);
    // Select the Item in the Combo
    comboBox1.SelectedIndex=index;
    }

Status

This behavior is by design.

More Information

Steps to Reproduce the Behavior

  1. In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, create a new Windows application by using Visual Basic .NET, Visual Basic 2005, or Visual C# .NET.

    By default, Form1 is created.Note You must change the code in Visual Basic 2005. By default, Visual Basic creates two files for the project when you create a Windows Forms project. If the form is named Form1, the two files that represent the form are named Form1.vb and Form1.Designer.vb. You write the code in the Form1.vb file. The Windows Forms Designer writes the code in the Form1.Designer.vb file. The Windows Forms Designer uses the partial keyword to divide the implementation of Form1 into two separate files. This behavior prevents the designer-generated code from being interspersed with your code.

    For more information about the new Visual Basic 2005 language enhancements, visit the following Microsoft Developer Network (MSDN) Web site:

    For more information about partial classes and the Windows Forms Designer, visit the following MSDN Web site:

  2. From the toolbox, drag a ComboBox to Form1.
  3. Right-click ComboBox1, and then click
    Properties.
  4. In the Properties window, click to select
    DropDownStyle, and then set the property to
    DropDownList.
  5. In the Properties window, click to select
    Items, and then click the ellipsis button (...).
  6. In String Collection Editor, type
    red, oak, and
    rose on different lines, and then click
    OK.
  7. On the Debug menu, click
    Start to run the application.
  8. Type the ro in ComboBox1.

References

For more information, visit the following MSDN Web site:

ComboBox.DropDownStyle Property
http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.dropdownstyle(vs.71).aspx
Vlastnosti

ID článku: 814362 - Poslední kontrola: 20. 2. 2009 - Revize: 1

Váš názor