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



The information in this article applies to:

  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • 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 Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • 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("Text typed in ComboBox is : " + ComboBox1.AccessibilityObject.Value)
Microsoft Visual C# .NET Code
System.Diagnostics.Debug.WriteLine("Text typed in ComboBox is : " + 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 "Applies to" section.

MORE INFORMATION


Steps to Reproduce the Behavior

  1. Create a new Windows application by using Visual Basic .NET or Visual C# .NET.

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

    Visual Basic .NET Code
        ' USSTates Table 
        Dim statesTable As New DataTable("USStates")
        ' Add a Column to the table
        statesTable.Columns.Add(New DataColumn("Name"))
    
        ' Add Rows to the table
        Dim dr As DataRow
        dr = statesTable.NewRow()
        dr(0) = "Washington"
        statesTable.Rows.Add(dr)
    
        dr = statesTable.NewRow()
        dr(0) = "West Virginia"
        statesTable.Rows.Add(dr)
    
        ' Bind the ComboBox using DataSource 
        ComboBox1.DataSource = statesTable.DefaultView
    
        ' Display the Name in the ComboBox
        ComboBox1.DisplayMember = "Name"
    Visual C# .NET Code
       // USSTates Table 
       DataTable statesTable = new DataTable("USStates");
       // Add a Column to the table
       statesTable.Columns.Add(new DataColumn("Name"));
    
       // Add Rows to the table
       DataRow  dr;
       dr = statesTable.NewRow();
       dr[0] = "Washington";
       statesTable.Rows.Add(dr);
    
       dr = statesTable.NewRow();
       dr[0] = "West Virginia";
       statesTable.Rows.Add(dr);
    
       // Bind the ComboBox using DataSource 
       comboBox1.DataSource = statesTable.DefaultView;
    
       // Display the Name in the ComboBox
       comboBox1.DisplayMember = "Name";
  4. Add the following code to the ComboBox1_TextChanged event.

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

    Notice the value that is printed in the output window.

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

327244 BUG: ComboBox does not clear when you set SelectedIndex to -1


Modification Type:MinorLast Reviewed:1/21/2006
Keywords:kbvs2005applies kbvs2005doesnotapply kbvs2002sp1sweep kbWindowsForms kbDataBinding kbControl kbComboBox kbCtrl kbbug KB814346 kbAudDeveloper