Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

CheckedListBox forgets on TabControl

16 views
Skip to first unread message

Greg Burns

unread,
May 13, 2002, 11:36:48 AM5/13/02
to
I have a CheckedListBox on a TabControl's TabPage that cannot remeber which
items are checked when I switch between TabPages.

I had populated that CheckedListBox uses databinding, like so...

CheckedListBox1.DataSource = ds.Tables(0)
CheckedListBox1.DisplayMember = "FullName"
CheckedListBox1.ValueMember = "user_id"

I suspected the databinding had something to do with the control forgetting
what was checked. So I redesigned to manually loop through a DataReader and
add the items to it.

This presented the problem that I need to display "FullName", but use it
value "user_id". So I created a structure, structItem to accomplish this.

Structure structItem
Dim Display As String
Private m_Value As String
Shadows ReadOnly Property ToString()
Get
Return Display.ToString
End Get
End Property
Property Value()
Get
Return m_Value.Trim.ToString
End Get
Set(ByVal Value)
m_Value = Value
End Set
End Property
End Structure


Do While dr.Read
Dim MyItem As New structItem()

With MyItem
.Display = dr("FullName")
.Value = dr("user_id")
End With

With CheckedListBox1.Items
.Add(MyItem.Display.ToString, False)
End With
Loop


Now how to I get back my "Value" property when referencing the Checked item?
With databinding I used this code:

For i = 0 To CheckedListBox1.CheckedItems.Count - 1
MsgBox(CheckedListBox1.CheckedItems.Item(i)("user_id"))
Next

I've tried doing something like this, but it doesn't work:

For i = 0 To CheckedListBox1.CheckedItems.Count - 1
MsgBox(CType(CheckedListBox1.CheckedItems.Item(i), structItem).Value)
Next

Help!
Greg

Steven Bras [MS]

unread,
May 13, 2002, 7:13:27 PM5/13/02
to
I would recommend using the following workaround to persist the checked
values, and leave the checked list box bound as you did first.

1. Add the following to set up a private array to hold the checked values
in the listbox:

Private oCheckedItems As DataRowView()

2. Prior to doing anything that will cause the listbox to become invisible,
save the state of the check boxes as follows:

ReDim oCheckedItems(CheckedListBox1.CheckedItems.Count - 1)
CheckedListBox1.CheckedItems.CopyTo(oCheckedItems, 0)

3. Just before the listbox becomes visible again (when user clicks the tab
containing the listbox), add the following code:

Dim x As Integer

For x = LBound(oCheckedItems) To UBound(oCheckedItems)
'Item(1) in the oCheckedItems(x) item is the display member; item(0)
would be the value member

CheckedListBox1.SetItemChecked(CheckedListBox1.FindStringExact(oCheckedItems
(x).Item(1).ToString, -1), True)
Next

This is clunky but may be useful if you find you cannot retrieve the data
from your structure.

Hope this helps!

Steven Bras, MCSD
Microsoft Developer Support/Visual Basic WebData

This posting is provided "AS IS" with no warranties, and confers no rights.

Are you secure? For information about the Microsoft Strategic Technology
Protection Program and to order your FREE Security Tool Kit, please visit
http://www.microsoft.com/security.

Greg Burns

unread,
May 13, 2002, 7:20:23 PM5/13/02
to
I don't seem to be the only experiencing this problem...
(http://groups.google.com/groups?hl=en&threadm=enhzxrncBHA.2680%40tkmsftngp0
7&rnum=24&prev=/groups%3Fq%3Dtabcontrol%2Bgroup:microsoft.public.dotnet.lang
uages.vb.*%26hl%3Den%26start%3D20%26sa%3DN)

Ideas?

Greg

Greg Burns

unread,
May 13, 2002, 7:22:37 PM5/13/02
to
Thanks Steven, I will give it a shot.

Is this a bug, or by design? :)

Greg

"Steven Bras [MS]" <stev...@online.microsoft.com> wrote in message
news:v5jsOPt#BHA.2264@cpmsftngxa07...

Greg Burns

unread,
May 13, 2002, 8:00:38 PM5/13/02
to
Here is part of the problem with my work-around code, I am adding just the
ToString result rather than MyItem structure.

Do While dr.Read
Dim MyItem As New structItem()

With MyItem
.Display = dr("FullName")
.Value = dr("user_id")
End With

With CheckedListBox1.Items
.Add(MyItem.Display.ToString, False) <<------
End With
Loop


But if I use this code...

With CheckedListBox1.Items
.Add(MyItem, False)
End With

It doesn't automatically display the result of ToString (like I was hoping),
instead it displays
"CheckedListBox.Form1+structItem"

grrr..

What is the benefit of being able to add any object type to a ListBox, if it
behaves like this? (In other words, what am I doing wrong!).

Greg

Steven Bras [MS]

unread,
May 14, 2002, 1:35:48 PM5/14/02
to
This will be logged as a bug, but it's up to the development team how to
classify it. Sorry for the trouble it's caused!
0 new messages