BUG: CheckedListBox Control Loses Check Marks When the Visible Property Is Changed to FALSE and Then Back to TRUE (327896)



The information in this article applies to:

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

This article was previously published under Q327896

SYMPTOMS

Check marks are cleared when the visibility of a CheckedListBox control changes. This only occurs when the control is data-bound.

CAUSE

This problem occurs because the CheckedListBox was not designed for data binding.

RESOLUTION

You can populate the CheckedListBox manually. You must not use data binding with the CheckedListBox.
   Dim DataTbl As New DataTable("DemoTable")
   Dim DataCol1 As New DataColumn()
   Dim DRow As DataRow
   Dim counter As Int32

   DataCol1.DataType = GetType(String)
   DataCol1.ColumnName = "Name"
   DataTbl.Columns.Add(DataCol1)

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

   Dim sqlDS As New DataSet()
   sqlDS.Tables.Add(DataTbl)

   For Each DRow In sqlDS.Tables("DemoTable").Rows
       Me.CheckedListBox1.Items.Add(DRow("Name"))
   Next
				

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

MORE INFORMATION

Steps to Reproduce the Behavior

  1. Create a new Windows Application.
  2. Add a TabControl control to the Form.
  3. Add two TabPage controls to the TabControl.
  4. Add CheckedListBox to one of the TabPage controls.
  5. Double-click the Form and then add the following code to the Forms Load event:
       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
    
       Dim sqlDS As New DataSet()
       sqlDS.Tables.Add(DataTbl)
    
       With CheckedListBox1
          .DataSource = sqlDS.Tables("DemoTable")
          .ValueMember = "ID"
          .DisplayMember = "Name"
       End With
    					
  6. Press F5 to compile and to run the application.
  7. Check items on the CheckedListBox.
  8. Toggle between one TabPage and the other TabPage.

    Notice that when you return to the TabPage that contains the CheckedListBox control, all check marks on the control are cleared.

Modification Type:MinorLast Reviewed:9/4/2003
Keywords:kbbug KB327896 kbAudDeveloper