Microsoft KB Archive/814346

= BUG: The Text property retains a previous value when you type the first character in a bound ComboBox =

Article ID: 814346

Article Last Modified on 1/21/2006

-

APPLIES TO


 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * Microsoft Visual Studio .NET 2002 Professional Edition
 * Microsoft Visual Studio .NET 2002 Academic Edition
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Professional Edition

-



SYMPTOMS
You bind a ComboBox control to the Datasource property, and then set the DisplayMember property of the ComboBox. The Text property of the ComboBox returns the previously selected item when you type the first character in the TextChange event. Thereafter, the Text property returns the correct value.



CAUSE
When you type the first character in the TextChange event, the SelectedItem property is not changed in the ComboBox. The ComboBox receives the previously selected item from the list. After you type the first character, the TextChanged event is fired. The ComboBox updates the SelectedIndex property after you set the values for TextChanged and SelectedItem to null, and you set the value for SelectedIndex to -1. If SelectedItem is absent, the WindowText property is retrieved. Therefore, the problem behavior only occurs one time.



WORKAROUND
To work around this problem, use the Value property of the AccessibleObject class instead of using the Text property of the ComboBox as follows:

Microsoft Visual Basic .NET Code System.Diagnostics.Debug.WriteLine(&quot;Text typed in ComboBox is : &quot; + ComboBox1.AccessibilityObject.Value) Microsoft Visual C# .NET Code System.Diagnostics.Debug.WriteLine(&quot;Text typed in ComboBox is : &quot; + ComboBox1.AccessibilityObject.Value);

Note To use AccesibleObject, add a reference to the Accessibility assembly.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section.



Steps to Reproduce the Behavior
 Create a new Windows application by using Visual Basic .NET or Visual C# .NET.

By default, Form1 is created. From the toolbox, drag a ComboBox onto Form1.  Double-click Form1 to open the following Code view for Form1.

Visual Basic .NET Code ' USSTates Table Dim statesTable As New DataTable(&quot;USStates&quot;) ' Add a Column to the table statesTable.Columns.Add(New DataColumn(&quot;Name&quot;))

' Add Rows to the table Dim dr As DataRow dr = statesTable.NewRow dr(0) = &quot;Washington&quot; statesTable.Rows.Add(dr)

dr = statesTable.NewRow dr(0) = &quot;West Virginia&quot; statesTable.Rows.Add(dr)

' Bind the ComboBox using DataSource ComboBox1.DataSource = statesTable.DefaultView

' Display the Name in the ComboBox ComboBox1.DisplayMember = &quot;Name&quot; Visual C# .NET Code // USSTates Table DataTable statesTable = new DataTable(&quot;USStates&quot;); // Add a Column to the table statesTable.Columns.Add(new DataColumn(&quot;Name&quot;));

// Add Rows to the table DataRow dr; dr = statesTable.NewRow; dr[0] = &quot;Washington&quot;; statesTable.Rows.Add(dr);

dr = statesTable.NewRow; dr[0] = &quot;West Virginia&quot;; statesTable.Rows.Add(dr);

// Bind the ComboBox using DataSource comboBox1.DataSource = statesTable.DefaultView;

// Display the Name in the ComboBox comboBox1.DisplayMember = &quot;Name&quot;;   Add the following code to the ComboBox1_TextChanged event.

Visual Basic .NET Code ' Print the changed values to Output Window System.Diagnostics.Debug.WriteLine(&quot;Text typed in ComboBox is : &quot; & ComboBox1.Text) Visual C# .NET Code // Print the changed values to Output Window System.Diagnostics.Debug.WriteLine(&quot;Text typed in ComboBox is : &quot; + comboBox1.Text);  On the Debug menu, click Start to run the application. Type a character in the ComboBox.</li> On the View menu, point to Other Windows, and then click Output.

Notice the value that is printed in the output window.</li></ol>

<div class="references_section">