Adding the Id of an item in CheckedListBox
Hi!
I am still new in VB.Net.
For Example I have a declaration
Public EmpId as String
I just wanted to ask help about saving the ID's of the checked items in CheckedListBox to my EmpId variable. The ID that I will be saving has a string data type.
I tried this code but i don't think it is right.
Dim o As Object
For o = 0 To chkListBoxEmployee.CheckedItems.Count - 1
MessageBox.Show(o.ToString)
Next o
Thank you.:afrog:
Re: Adding the Id of an item in CheckedListBox
If there are multiple checked items then how are you going to store all of them in a single String variable? Are you planning on concatenating them altogether, maybe delimited by commas or something? If not then you'd need a String() or a List(Of String) rather than just a String. Here's how you'd get all the checked values into a String(), i.e. a String array:
vb.net Code:
Dim upperBound As Integer = Me.CheckedListBox1.CheckedItems.Count - 1
Dim checkedItems(upperBound) As String
For index As Integer = 0 To upperBound
checkedItems(index) = CStr(Me.CheckedListBox1.CheckedItems(index))
Next
If you then wanted all those values in one String you could do it like this:
vb.net Code:
Dim allValues As String = String.Join(",", checkedItems)
Re: Adding the Id of an item in CheckedListBox
I also have this code:
Dim o As Object
For Each o In chkListBoxEmployee.CheckedItems
MessageBox.Show(o.ToString)
Next
The items returned are the names of the employees that is checked.
How can I return the ID's of the employees which is of string datatype.
Re: Adding the Id of an item in CheckedListBox
Ok, you've been holding information back then. You didn't tell us in the first place that the information displayed in the CheckedListBox wasn't the data you wanted to get. If you don't tell us then we don't know. In future, please provide a full and clear description of what you're trying to achieve and how you're trying to achieve it.
So, how exactly does the data get into the CheckedListBox? Is it bound? If so, what type of list is the control bound to? If it's not bound, where are the ID values stored and what is their relationship to the names displayed in the control?