Microsoft KB Archive/327244

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

Article ID: 327244

Article Last Modified on 1/12/2006

-

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(&quot;DemoTable&quot;) Dim DataCol1 As New DataColumn Dim DataCol2 As New DataColumn Dim DRow As DataRow Dim counter As Int32

DataCol1.DataType = GetType(Int32) DataCol1.ColumnName = &quot;ID&quot; DataCol1.AutoIncrement = True DataTbl.Columns.Add(DataCol1) Dim Key(0) As DataColumn Key(0) = DataCol1 DataTbl.PrimaryKey = Key

DataCol2.DataType = GetType(String) DataCol2.ColumnName = &quot;CompanyName&quot; DataTbl.Columns.Add(DataCol2)

For counter = 0 To 10 DRow = DataTbl.NewRow DRow(&quot;CompanyName&quot;) = &quot;John &quot; & 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(&quot;CompanyName&quot;)) Next 



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.</li> Add a ComboBox and a Button control to the form.</li>  Double-click the form and then enter the following code on the Load event of the form: Dim DataTbl As New DataTable(&quot;DemoTable&quot;) Dim DataCol1 As New DataColumn Dim DataCol2 As New DataColumn Dim DRow As DataRow Dim counter As Int32

DataCol1.DataType = GetType(Int32) DataCol1.ColumnName = &quot;ID&quot; DataCol1.AutoIncrement = True DataTbl.Columns.Add(DataCol1) Dim Key(0) As DataColumn Key(0) = DataCol1 DataTbl.PrimaryKey = Key

DataCol2.DataType = GetType(String) DataCol2.ColumnName = &quot;Name&quot; DataTbl.Columns.Add(DataCol2)

For counter = 0 To 10 DRow = DataTbl.NewRow DRow(&quot;Name&quot;) = &quot;John &quot; & counter.ToString DataTbl.Rows.Add(DRow) Next

With ComboBox1 .DataSource = DataTbl .ValueMember = &quot;ID&quot; .DisplayMember = &quot;Name&quot; End With </li>  Double-click the Button to insert code for the Click event: ComboBox1.SelectedIndex = -1 </li> Press F5 to run the application.</li> 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.</li></ol>

Keywords: kbbug kbnofix KB327244

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.