Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checked Listbox Quandry

Status
Not open for further replies.

DerPflug

Programmer
Mar 28, 2002
153
US
I'm loading a checked listbox dynamically from a list of statuses from a table with similar values:
Code:
ID     Description
1        Pending
2        Done
3        Processing
4        Dead
          etc.
I have another table with a pipe-delimited string that is a list of default statuses to be checked: '2|3|8'

I have loaded the checked listbox with the Description showing as the text and have each selection checked as listed in the pipe-delimited string. However, I also want to be able to change these values (check different ones) and save these to the database. My goal is to create a new pipe-delimted string of these ID's and update the database. My problem is that I need to have a correspondence between the ID and Description within the listbox. Is there another property that I can set for each check box item where I can store the ID?

thanks in advance for any help
 
You can create a class with two fields and an overriden ToString Method and add that to the listbox. The ToString Method would return the Description and when you need to get the ID all you need to do is convert the object back to your custom class type.

Example Class:
Code:
Public Class MyListItem
  Public Sub New(byval id as Integer, byval description as String)
    Me.ID = id
    Me.Description = description
  End Sub

  Public ID as Integer
  Public Description as String
 
  Public Overrides Function ToString() as String
    return Description
  End Function
End Class

To use this class your code would looke something like this.
Code:
' Adds an item to the checked list box      
Me.CheckedListBox1.Items.Add(New MyListItem(1, "MyDescription"), True)

For i As Integer = 0 To CheckedListBox1.SelectedItems.Count - 1
  Dim listItem As MyListItem
  ' Converts the selected item back to your custom class
  listItem = CType(CheckedListBox1.SelectedItems(i), MyListItem)
  MsgBox(String.Format("My Database ID='{0}' for '{1}'", listItem.ID, listItem.Description))
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top