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

만료된 KB 콘텐츠 고지 사항

이 문서는 Microsoft에서 더 이상 지원하지 않는 제품에 대해 작성되었습니다. 따라서 이 문서는 “있는 그대로" 제공되며 더 이상 업데이트되지 않습니다.

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.
속성

문서 ID: 327244 - 마지막 검토: 2006. 1. 12. - 수정: 1

피드백