CheckedListBox State

G

Guest

Hi,

We are populating a CheckedListBox from a database. Based on a selection by the user, the CheckedListBox either contains ALL possible members of a set, or a subset of possible members. The user can click a button to toggle back-and-forth between showing all or some. The question we have is maintaining state of the CheckedListBox. In other words, if a user has selected "Show MY records" and checks a few boxes, then selects "Show ALL records", we want the items the user has already checked to still be checked when the CheckedListBox is redisplayed.

We have tried many approaches to this with no success. Any help will be GREATLY appreciated. Thank you.
 
P

Peter Huang

Hi ,

Thanks for posting in the community.

First of all, I would like to confirm my understanding of your issue.
From your description, I understand that after you populate a databinding
checkedlistbox from a database, when you redisplay the checkedlistbox, the
checked state was not persists.
Have I fully understood you? If there is anything I misunderstood, please
feel free to let me know.

What do you mean by "redisplay", do you mean set its visible property to
false and then true again?

If so you may take a look at the KB link below.
This is an known issue, as a workaround you may need to do the persist
stuff yourself.
833033 BUG: A checked list box that you bind to a data table in a TabControl
http://support.microsoft.com/?id=833033

Or you can you simple your code and post here for me to reproduce?


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
P

Peter Huang

Hi,

Did my last answer help you?
In addition, do you mean when you populate the checkedlistbox and then
check some items and then you repopulate the checkedlistbox, you wants to
persists the checked state.

I think you may still need to persist the checked state yoursefl.
Here is a sample, you may have a look.

Dim checkedList As Collection
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
'Clear the Last Saved Checkedstate List
checkedList = New Collection

'Save the Current Checkedstate List
For Each o As Object In Me.CheckedListBox1.CheckedItems
checkedList.Add(o.ToString()) 'the string must be unique, or
you need to select another value to unique identity the item
Next
'RePopulate the checkListBox
Me.CheckedListBox1.Items.Clear()
For i As Integer = 1 To 3

Me.CheckedListBox1.Items.Add(DataSet11.Table1.Rows(i).Item(0).ToString())
Next

'Restore the Last Saved Checkedstate List
For Each o As String In checkedList
Dim i As Integer =
Me.CheckedListBox1.FindStringExact(o.ToString())
If Not i = -1 Then
CheckedListBox1.SetItemChecked(i, True)
End If
Next
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.SqlDataAdapter1.Fill(Me.DataSet11)
For Each o As DataRow In DataSet11.Table1.Rows
Me.CheckedListBox1.Items.Add(o.Item(0).ToString())
Next
End Sub


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top