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
STATUSMicrosoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.
Modification Type: | Major | Last Reviewed: | 1/12/2006 |
---|
Keywords: | kbbug kbnofix KB327244 kbAudDeveloper |
---|
|