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



The information in this article applies to:

  • Microsoft Common Language Runtime (included with the .NET Framework) 1.0

This article was previously published under Q327244

SYMPTOMS

When you set the SelectedIndex property of a data-bound Windows Forms ComboBox control equal to -1, the selected item does not clear from the ComboBox.

When the value of -1 is assigned to the property for the first time, the first item at index 0 (zero) is selected in the ComboBox. When the value of -1 is assigned to the property for the second time, this assignment causes the ComboBox to clear.

CAUSE

When you assign -1 to the SelectedIndex property of the ComboBox for the first time, this causes the item at index 0 to be selected. If the item at index 0 is already selected, the ComboBox clears.

RESOLUTION

To work around this issue, use one of the following methods:
  • Method 1
    Use the following code to assign the value -1 to the SelectedIndex two times in a row:
    ComboBox1.SelectedIndex = -1
    ComboBox1.SelectedIndex = -1
    					
  • Method 2
    Use the following code to assign the value 0 to the SelectedIndex first, followed by the value -1:
    ComboBox1.SelectedIndex = 0
    ComboBox1.SelectedIndex = -1
    					
  • Method 3
    Use the following code to add the items to the control manually instead of binding to a datasource:
       Dim DataTbl As New DataTable("DemoTable")
       Dim DataCol1 As New DataColumn()
       Dim DataCol2 As New DataColumn()
       Dim DRow As DataRow
       Dim counter As Int32
    
       DataCol1.DataType = GetType(Int32)
       DataCol1.ColumnName = "ID"
       DataCol1.AutoIncrement = True
       DataTbl.Columns.Add(DataCol1)
       
       Dim Key(0) As DataColumn
       Key(0) = DataCol1
       DataTbl.PrimaryKey = Key
    
       DataCol2.DataType = GetType(String)
       DataCol2.ColumnName = "CompanyName"
       DataTbl.Columns.Add(DataCol2)
    
       For counter = 0 To 10
          DRow = DataTbl.NewRow()
          DRow("CompanyName") = "John " & counter.ToString
          DataTbl.Rows.Add(DRow)
       Next
    
       ' Populate the ComboBox manually
       Dim dr As DataRow
       For Each dr In DataTbl.Rows
          ComboBox1.Items.Add(dr("CompanyName"))
       Next
    					

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.
  2. Add a ComboBox and a Button control to the form.
  3. Double-click the form and then enter the following code on the Load event of the form:
       Dim DataTbl As New DataTable("DemoTable")
       Dim DataCol1 As New DataColumn()
       Dim DataCol2 As New DataColumn()
       Dim DRow As DataRow
       Dim counter As Int32
    
       DataCol1.DataType = GetType(Int32)
       DataCol1.ColumnName = "ID"
       DataCol1.AutoIncrement = True
       DataTbl.Columns.Add(DataCol1)
       
       Dim Key(0) As DataColumn
       Key(0) = DataCol1
       DataTbl.PrimaryKey = Key
    
       DataCol2.DataType = GetType(String)
       DataCol2.ColumnName = "Name"
       DataTbl.Columns.Add(DataCol2)
    
       For counter = 0 To 10
          DRow = DataTbl.NewRow()
          DRow("Name") = "John " & counter.ToString
          DataTbl.Rows.Add(DRow)
       Next
    
       With ComboBox1
          .DataSource = DataTbl 
          .ValueMember = "ID"
          .DisplayMember = "Name"
       End With
    					
  4. Double-click the Button to insert code for the Click event:
    ComboBox1.SelectedIndex = -1
    					
  5. Press F5 to run the application.
  6. Select an item, other than the first one, from the ComboBox and then press the Button on the form.

    The first item now appears in the ComboBox. If you press the Button a second time, the ComboBox clears.

Modification Type:MajorLast Reviewed:1/12/2006
Keywords:kbbug kbnofix KB327244 kbAudDeveloper