Microsoft KB Archive/833033

= 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 =

Article ID: 833033

Article Last Modified on 4/29/2007

-

APPLIES TO


 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * 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.  Use Microsoft Visual Basic .NET to create a Windows Application project. By default, the Form1.vb form is created. Add a TabControl control to the Form1 form. By default, the TabControl1 control is created. Right-click TabControl1, and then click Add Tab. By default, the TabPage1 control is created. Right-click TabControl1, and then click Add Tab. By default, the TabPage2 control is created. Add a CheckedListBox control to the TabPage2 control. By default, the CheckedListBox1 control is created. Add a DataSet control to the TabPage2 control. In the Add Dataset dialog box that appears, click Untyped dataset, and then click OK.</li>  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(&quot;Time&quot;) 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 = &quot;Time&quot; checkboxCount = CheckedListBox1.Items.Count </li>  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 </li>  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 </li></ol>

The complete code listing is as follows: Public Class Form1 Inherits System.Windows.Forms.Form


 * 1) Region &quot; Windows Form Designer generated code &quot;

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 = &quot;TabControl1&quot; 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 = &quot;TabPage1&quot; Me.TabPage1.Size = New System.Drawing.Size(264, 190) Me.TabPage1.TabIndex = 0 Me.TabPage1.Text = &quot;TabPage1&quot; '       'CheckBox1 '       Me.CheckBox1.Location = New System.Drawing.Point(8, 24) Me.CheckBox1.Name = &quot;CheckBox1&quot; Me.CheckBox1.Size = New System.Drawing.Size(96, 24) Me.CheckBox1.TabIndex = 0 Me.CheckBox1.Text = &quot;CheckBox1&quot; '       'TabPage2 '       Me.TabPage2.Controls.Add(Me.CheckedListBox1) Me.TabPage2.Location = New System.Drawing.Point(4, 22) Me.TabPage2.Name = &quot;TabPage2&quot; Me.TabPage2.Size = New System.Drawing.Size(264, 190) Me.TabPage2.TabIndex = 1 Me.TabPage2.Text = &quot;TabPage2&quot; '       'CheckedListBox1 '       Me.CheckedListBox1.Location = New System.Drawing.Point(8, 8) Me.CheckedListBox1.Name = &quot;CheckedListBox1&quot; Me.CheckedListBox1.Size = New System.Drawing.Size(136, 34) Me.CheckedListBox1.TabIndex = 0 '       'DataSet1 '       Me.DataSet1.DataSetName = &quot;NewDataSet&quot; Me.DataSet1.Locale = New System.Globalization.CultureInfo(&quot;en-US&quot;) '       'Form1 '       Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.Add(Me.TabControl1) Me.Name = &quot;Form1&quot; Me.Text = &quot;Form1&quot; Me.TabControl1.ResumeLayout(False) Me.TabPage1.ResumeLayout(False) Me.TabPage2.ResumeLayout(False) CType(Me.DataSet1, System.ComponentModel.ISupportInitialize).EndInit Me.ResumeLayout(False)

End Sub


 * 1) 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(&quot;Time&quot;) 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 = &quot;Time&quot; 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

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed in the &quot;Applies to&quot; section of this article.

<div class="moreinformation_section">

Steps to reproduce the behavior
<ol> Use Visual Basic .NET to create a Windows Application project.</li> Add a TabControl control to the Form1 form. By default, the TabControl1 control is created.</li> Right-click TabControl1, and then click Add Tab. By default, the TabPage1 control is created.</li> Right-click TabControl1, and then click Add Tab. By default, the TabPage2 control is created.</li> Add a CheckedListBox control to the TabPage2 control. By default, the CheckedListBox1 control is created.</li> Drag a DataSet control to the TabPage2 control.</li> In the Add Dataset dialog box that appears, click Untyped dataset, and then click OK.</li> Double-click Form1 to create the Form1_Load event handler.</li>  In the Form1_Load event handler, add the following code: dataset1.tables.add dataset1.tables(0).columns.add(&quot;Time&quot;) dim times as string = {timeofday} dataset1.tables(0).rows.add(times) checkedlistbox1.datasource = dataset1.tables(0) checkedlistbox1.displaymember = &quot;Time&quot; </li> On the Build menu, click Start. By default, the Form1 form appears.</li> Switch to the tab page that contains the CheckedListBox1 control.</li> Click to select the items in the CheckedListBox1 control.</li> 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 &quot;Symptoms&quot; section of this article.</li></ol>

<div class="references_section">