Results 1 to 4 of 4

Thread: How to Loop through Listview items

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    44

    How to Loop through Listview items

    I need some kind of loop, I think, to put into a variable for inclusion in access db. The problem I have at the minute is that with the code I am using, it gets the first value and if I click on another item, it retains the old value and dosen't update with new value. I need to get the items in the listview not the selectedItems.

    How can I create a loop that will store the values from selected items. Thanks

    Code:
    msg = CStr(lvSelectedItems.Items.Count)
    
            If CDbl(msg) > 0 Then
    
                'With lvSelectedItems.Items
    
                For Each item As ListViewItem In Me.lvSelectedItems.Items
    
                    Dim username As String = lvSelectedItems.Items.Item(0).Text
                    Dim session As String = lvSelectedItems.Items.Item(0).SubItems.Item(1).Text
    
                    output = username + " : " + session
    
                Next
                MessageBox.Show(output)
    
                'End With

  2. #2
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    351

    Re: How to Loop through Listview items

    You need to ADD to the output. You are currently replacing it each time:

    Code:
    output += username + " : " + session
    Also, counts are integers, you dont need to convert it to a double:

    Code:
    If lvSelectedItems.Items.Count > 0
    HTH
    Rico

    Using: VB.net & MS SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Sep 2013
    Posts
    44

    Re: How to Loop through Listview items

    Thank you for that enrico. However, what is happening, is that the results are all being joined together, which will make it difficult for access db inclusion. How do I seperate the results or create an array to split the items.

    Here is the latest code I am using. Thanks


    Code:
      
                If CDbl(msg) > 0 Then
    
                Dim username As String
                Dim session As String
                For Each item As ListViewItem In Me.lvSelectedItems.Items
                    username = item.Text
                    session = item.SubItems.Item(1).Text
                    output1 += username
                    output2 += session
    
    
                Next
    
                End If

  4. #4
    Hyperactive Member
    Join Date
    Jan 2007
    Posts
    351

    Re: How to Loop through Listview items

    What you do depends on what you want to use it for. How are you intending to use username and session?

    Lists would be one example:

    Code:
            If lvSelectedItems.Items.Count > 0 Then
    
                Dim Usernames As New List(of String)
                Dim Sessions As New List(of String)
    
                For Each item As ListViewItem In Me.lvSelectedItems.Items
    
                    Usernames.add(item.Text)
                    Sessions.add(item.SubItems.Item(1).Text)
    
                Next
    
             End If
    HTH
    Rico

    Using: VB.net & MS SQL

Tags for this Thread

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