Live Chat Icon For mobile
Live Chat Icon

Why does the CheckedListBox lose checked state when placed in a tab page that gets hidden and shown?

Platform: WinForms| Category: Data Binding

In Usenet posts, MS has acknowledged this bug. The problem is essentially that any time the visibility changes on a CheckedListBox, it loses its previous selections. Naturally this happens all the time in tab controls when changing tabs.

This derived Control saves it’s state while getting hidden and reloads it while getting shown:


[C#]
# region Workaround for CheckedListBox bug
		//////////////////////////////////////////////////////////////////////
		// When CheckedList box becomes invisible (e.g. when the tab page
		// containing this control is overlapped with another tab page),
		// checked state of the items are getting lost. This workaround will
		// fix this problem
		//////////////////////////////////////////////////////////////////////

		private bool[] isItemChecked;
		protected override void OnDataSourceChanged(EventArgs e)
		{
			base.OnDataSourceChanged(e);

			int cnt = this.Items.Count;
			isItemChecked = new Boolean[cnt];
			for(int i = 0; i < cnt; i++)
			{
				isItemChecked[i] = GetItemChecked(i);
			}
		}

		protected override void OnItemCheck(ItemCheckEventArgs e)
		{
			base.OnItemCheck(e);
			isItemChecked[e.Index] = (e.NewValue == CheckState.Checked);
		}

		protected override void OnVisibleChanged(EventArgs e)
		{
			base.OnVisibleChanged(e);

			if (this.Visible == true)
			{
				// Reset the checked states when it becomes visible.
				for(int i =0; i < this.Items.Count; i++)
				{
					SetItemChecked(i, isItemChecked[i]);
				}
			}
		}
#endregion


[VB.Net]
Public Class MyCheckedListBox
   Inherits CheckedListBox
    ’////////////////////////////////////////////////////////////////////
    ’ When CheckedList box becomes invisible (e.g. when the tab page
    ’ containing this control is overlapped with another tab page),
    ’ checked state of the items are getting lost. This workaround will
    ’ fix this problem
    ’////////////////////////////////////////////////////////////////////
   Private isItemChecked() As Boolean
   
   Protected Overrides Sub OnDataSourceChanged(e As EventArgs)
      MyBase.OnDataSourceChanged(e)
      
      Dim cnt As Integer = Me.Items.Count
      isItemChecked = New [Boolean](cnt) {}
      Dim i As Integer
      For i = 0 To cnt - 1
         isItemChecked(i) = GetItemChecked(i)
      Next i
   End Sub ’OnDataSourceChanged
   
   
   Protected Overrides Sub OnItemCheck(e As ItemCheckEventArgs)
      MyBase.OnItemCheck(e)
      isItemChecked(e.Index) = e.NewValue = CheckState.Checked
   End Sub ’OnItemCheck
   
   
   Protected Overrides Sub OnVisibleChanged(e As EventArgs)
      MyBase.OnVisibleChanged(e)
      
      If Me.Visible = True Then
         ’ Reset the checked states when it becomes visible.
         Dim i As Integer
         For i = 0 To (Me.Items.Count) - 1
            SetItemChecked(i, isItemChecked(i))
         Next i
      End If
   End Sub ’OnVisibleChanged

End Class ’MyCheckedListBox 

(Contributed by Eric and Reddy Duggempudi in Syncfusion Windows Forms FAQ forums).

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.