Results 1 to 4 of 4

Thread: Adding the Id of an item in CheckedListBox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    105

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim upperBound As Integer = Me.CheckedListBox1.CheckedItems.Count - 1
    2. Dim checkedItems(upperBound) As String
    3.  
    4. For index As Integer = 0 To upperBound
    5.     checkedItems(index) = CStr(Me.CheckedListBox1.CheckedItems(index))
    6. Next
    If you then wanted all those values in one String you could do it like this:
    vb.net Code:
    1. Dim allValues As String = String.Join(",", checkedItems)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2008
    Posts
    105

    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.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width