BUG: A checked list box that you bind to a data table in a TabControl object does not retain the states of checked items when you switch tab pages (833033)



The information in this article applies to:

  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

SYMPTOMS

You may create a TabControl object that has a tab page that contains a checked list box that you bind to a data table. When you switch between the tab pages of this TabControl object, you notice that the checked list box does not retain the states of the checked items.

WORKAROUND

To work around this behavior, follow these steps:

Note The following procedure is valid only for applications that use the Microsoft .NET Framework version 1.1. If your application uses the .NET Framework version 1.0, the following procedure will not work.
  1. Use Microsoft Visual Basic .NET to create a Windows Application project. By default, the Form1.vb form is created.
  2. Add a TabControl control to the Form1 form. By default, the TabControl1 control is created.
  3. Right-click TabControl1, and then click Add Tab. By default, the TabPage1 control is created.
  4. Right-click TabControl1, and then click Add Tab. By default, the TabPage2 control is created.
  5. Add a CheckedListBox control to the TabPage2 control. By default, the CheckedListBox1 control is created.
  6. Add a DataSet control to the TabPage2 control.
  7. In the Add Dataset dialog box that appears, click Untyped dataset, and then click OK.
  8. To create multiple items in the CheckedListBox1 control of the TabPage2 control, add the following code in the Form1_Load event handler:
            DataSet1.Tables.Add()
            DataSet1.Tables(0).Columns.Add("Time")
            Dim times As String() = {TimeOfDay}
            Dim times1 As String() = {TimeOfDay}
            DataSet1.Tables(0).Rows.Add(times)
            DataSet1.Tables(0).Rows.Add(times1)
            CheckedListBox1.DataSource = DataSet1.Tables(0)
            CheckedListBox1.DisplayMember = "Time"
            checkboxCount = CheckedListBox1.Items.Count()
  9. To store the states of the selected items in the CheckedListBox1 control of the TabPage2 control, add the following code in the CheckedListBox1_SelectedIndexChanged event handler:
            ReDim Array1(checkboxCount - 1)
            Dim i As Integer
            For i = 0 To Array1.Length - 1
                Dim j As Boolean = CheckedListBox1.GetItemChecked(i)
                Array1(i) = j.ToString
            Next
  10. To restore the states of the selected items, add the following code in the TabControl1_Click event handler:
            For k = 0 To Array1.Length - 1
                CheckedListBox1.SetItemChecked(k, Array1(k))
            Next
The complete code listing is as follows:
    Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
    Private checkboxCount As Integer
    Private k As Integer


    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
    Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
    Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
    Friend WithEvents CheckedListBox1 As System.Windows.Forms.CheckedListBox
    Friend WithEvents CheckBox1 As System.Windows.Forms.CheckBox
    Friend WithEvents DataSet1 As System.Data.DataSet
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TabControl1 = New System.Windows.Forms.TabControl
        Me.TabPage1 = New System.Windows.Forms.TabPage
        Me.CheckBox1 = New System.Windows.Forms.CheckBox
        Me.TabPage2 = New System.Windows.Forms.TabPage
        Me.CheckedListBox1 = New System.Windows.Forms.CheckedListBox
        Me.DataSet1 = New System.Data.DataSet
        Me.TabControl1.SuspendLayout()
        Me.TabPage1.SuspendLayout()
        Me.TabPage2.SuspendLayout()
        CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'TabControl1
        '
        Me.TabControl1.Controls.Add(Me.TabPage1)
        Me.TabControl1.Controls.Add(Me.TabPage2)
        Me.TabControl1.Location = New System.Drawing.Point(8, 24)
        Me.TabControl1.Name = "TabControl1"
        Me.TabControl1.SelectedIndex = 0
        Me.TabControl1.Size = New System.Drawing.Size(272, 216)
        Me.TabControl1.TabIndex = 1
        '
        'TabPage1
        '
        Me.TabPage1.Controls.Add(Me.CheckBox1)
        Me.TabPage1.Location = New System.Drawing.Point(4, 22)
        Me.TabPage1.Name = "TabPage1"
        Me.TabPage1.Size = New System.Drawing.Size(264, 190)
        Me.TabPage1.TabIndex = 0
        Me.TabPage1.Text = "TabPage1"
        '
        'CheckBox1
        '
        Me.CheckBox1.Location = New System.Drawing.Point(8, 24)
        Me.CheckBox1.Name = "CheckBox1"
        Me.CheckBox1.Size = New System.Drawing.Size(96, 24)
        Me.CheckBox1.TabIndex = 0
        Me.CheckBox1.Text = "CheckBox1"
        '
        'TabPage2
        '
        Me.TabPage2.Controls.Add(Me.CheckedListBox1)
        Me.TabPage2.Location = New System.Drawing.Point(4, 22)
        Me.TabPage2.Name = "TabPage2"
        Me.TabPage2.Size = New System.Drawing.Size(264, 190)
        Me.TabPage2.TabIndex = 1
        Me.TabPage2.Text = "TabPage2"
        '
        'CheckedListBox1
        '
        Me.CheckedListBox1.Location = New System.Drawing.Point(8, 8)
        Me.CheckedListBox1.Name = "CheckedListBox1"
        Me.CheckedListBox1.Size = New System.Drawing.Size(136, 34)
        Me.CheckedListBox1.TabIndex = 0
        '
        'DataSet1
        '
        Me.DataSet1.DataSetName = "NewDataSet"
        Me.DataSet1.Locale = New System.Globalization.CultureInfo("en-US")
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 266)
        Me.Controls.Add(Me.TabControl1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.TabControl1.ResumeLayout(False)
        Me.TabPage1.ResumeLayout(False)
        Me.TabPage2.ResumeLayout(False)
        CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region



    Dim Array1() As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        DataSet1.Tables.Add()
        DataSet1.Tables(0).Columns.Add("Time")
        Dim times As String() = {TimeOfDay}
        Dim times1 As String() = {TimeOfDay}
        DataSet1.Tables(0).Rows.Add(times)
        DataSet1.Tables(0).Rows.Add(times1)
        CheckedListBox1.DataSource = DataSet1.Tables(0)
        CheckedListBox1.DisplayMember = "Time"
        checkboxCount = CheckedListBox1.Items.Count()

    End Sub

    Private Sub CheckedListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedListBox1.SelectedIndexChanged

        ReDim Array1(checkboxCount - 1)
        Dim i As Integer
        For i = 0 To Array1.Length - 1
            Dim j As Boolean = CheckedListBox1.GetItemChecked(i)
            Array1(i) = j.ToString
        Next
    End Sub

    Private Sub TabControl1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.Click
        For k = 0 To Array1.Length - 1
            CheckedListBox1.SetItemChecked(k, Array1(k))
        Next
    End Sub
End Class

STATUS

Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section of this article.

MORE INFORMATION

Steps to reproduce the behavior

  1. Use Visual Basic .NET to create a Windows Application project.
  2. Add a TabControl control to the Form1 form. By default, the TabControl1 control is created.
  3. Right-click TabControl1, and then click Add Tab. By default, the TabPage1 control is created.
  4. Right-click TabControl1, and then click Add Tab. By default, the TabPage2 control is created.
  5. Add a CheckedListBox control to the TabPage2 control. By default, the CheckedListBox1 control is created.
  6. Drag a DataSet control to the TabPage2 control.
  7. In the Add Dataset dialog box that appears, click Untyped dataset, and then click OK.
  8. Double-click Form1 to create the Form1_Load event handler.
  9. In the Form1_Load event handler, add the following code:
     dataset1.tables.add()
     dataset1.tables(0).columns.add("Time")
     dim times as string() = {timeofday}
     dataset1.tables(0).rows.add(times)
     checkedlistbox1.datasource = dataset1.tables(0)
     checkedlistbox1.displaymember = "Time"
  10. On the Build menu, click Start. By default, the Form1 form appears.
  11. Switch to the tab page that contains the CheckedListBox1 control.
  12. Click to select the items in the CheckedListBox1 control.
  13. Switch to the other tab page, and then switch to the tab page that contains the CheckedListBox1 control. You notice the behavior that is mentioned in the "Symptoms" section of this article.

REFERENCES

For more information about the TabControl class, visit the following Microsoft Developer Network (MSDN) Web site:

Modification Type:MinorLast Reviewed:9/15/2005
Keywords:kbvs2002sp1sweep kbUser kbAppDev kbcode kbbug KB833033 kbAudDeveloper