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
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.
Ideas?
Greg
Is this a bug, or by design? :)
Greg
"Steven Bras [MS]" <stev...@online.microsoft.com> wrote in message
news:v5jsOPt#BHA.2264@cpmsftngxa07...
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