Microsoft KB Archive/814362

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

Article ID: 814362

Article Last Modified on 5/11/2007

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





Beta Information
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.



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:  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. From the toolbox, drag a Timer to Form1. In the Properties window, set the Enabled property to True .  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;   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;   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 = &quot;&quot; Visual C# .NET //empty the string for every 1 seconds searchstr=&quot;&quot;; </li>  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; } </li></ol>

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

Steps to Reproduce the Behavior
<ol> 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:

http://msdn2.microsoft.com/en-us/library/ms379584(vs.80).aspx

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

http://msdn2.microsoft.com/en-us/library/ms171843.aspx

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

<div class="references_section">