Results 1 to 14 of 14

Thread: Why does this not work (Finding item in listview)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Why does this not work (Finding item in listview)

    So guys I'm using this code to find if there is any items in a listview

    Code:
    Dim i As Long, ItemFound As Boolean
    For i = 1 To Form9.ListView1.ListItems.Count
        If Form9.ListView1.ListItems(i).SubItems(2) = Format$(Now, "dd/mm/yyyy") And Form9.ListView1.ListItems(i).SubItems(3) >= "09:00" And Form9.ListView1.ListItems(i).SubItems(3) <= "09:29" Then
        Form9.ListView1.ListItems(i).Selected = True
            ItemFound = True
            Exit For
        Else
            ItemFound = False
        End If
    Next i
    If ItemFound = True Then
      
        Combo1.Visible = True
        Combo1.Text = Form9.ListView1.SelectedItem
        Combo1.AddItem (Form9.ListView1.SelectedItem)
    between the times of 09:00 and 09:30

    if so, I want it to add them to combo1.text

    for some reason at the moment the if item found code does not work as combo1 does not become visible,

    Any ideas why?

    Thanks,
    Jamie

  2. #2
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,269

    Re: Why does this not work (Finding item in listview)

    Have you single-stepped through your loop, if your code is really finding an item according to your criteria?
    The only reason your combo-box doesn't show up, is if ItemFound is False

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this not work (Finding item in listview)

    Hi mate, it's started to work, how ever only finds one item. How can i make it check if there is more than one and if so add it in on form load?

    Thanks
    Jamie

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this not work (Finding item in listview)

    Hi mate, it's started to work, how ever only finds one item. How can i make it check if there is more than one and if so add it in on form load?

    Thanks
    Jamie

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,269

    Re: Why does this not work (Finding item in listview)

    vb Code:
    1. Dim i As Long, ItemFound As Boolean
    2. For i = 1 To Form9.ListView1.ListItems.Count
    3.     If Form9.ListView1.ListItems(i).SubItems(2) = Format$(Now, "dd/mm/yyyy") And Form9.ListView1.ListItems(i).SubItems(3) >= "09:00" And Form9.ListView1.ListItems(i).SubItems(3) <= "09:29" Then
    4.          Form9.ListView1.ListItems(i).Selected = True
    5.          Combo1.Visible = True 'If at least one item is found
    6.          Combo1.Text = Form9.ListView1.SelectedItem
    7.          Combo1.AddItem (Form9.ListView1.SelectedItem)
    8.     Else
    9.         ItemFound = False
    10.     End If
    11. Next i

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this not work (Finding item in listview)

    Thanks mate, that works however only lists one item found in the combo box, how do i make it display each result found and then add it to the combo box?

    Thanks,
    Jamie

  7. #7
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,269

    Re: Why does this not work (Finding item in listview)

    Could you post your contents of your ListView?
    Is your Combobox in Dropdown-Style?

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this not work (Finding item in listview)

    So the contents of the listview are :

    Joe Bloggs | 11/09/2012 | 09:15

    Joe Bloggs1 | 11/09/2012 | 09:22


    Joe Bloggs2 | 11/09/2012 | 10:15


    At the moment it only display the first one rather than the first 2 (as they are both within 09:00 and 09:30)


    And yes it is dropdown.

    Cheers

  9. #9
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,269

    Re: Why does this not work (Finding item in listview)

    And you're sure, that the second item is not in the dropdown-part of the combo-box?
    btw: Delete Line 6 in my sample-code above. Maybe you'll see something

  10. #10
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,206

    Re: Why does this not work (Finding item in listview)

    Rather than
    Code:
    Combo1.Text = Form9.ListView1.SelectedItem
    Combo1.AddItem (Form9.ListView1.SelectedItem)
    Try
    Code:
    Combo1.Text = Form9.ListView1.ListItems(i).SubItems(3)
    Combo1.AddItem (Form9.ListView1.ListItems(i).SubItems(3))

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this not work (Finding item in listview)

    Nope only showing the first one

  12. #12
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,269

    Re: Why does this not work (Finding item in listview)

    Then there is something wrong with your entry in the ListView.

    If your If-Clause finds the first entry, but not the second one, then something's wrong with the entry and not the code.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    May 2009
    Posts
    876

    Re: Why does this not work (Finding item in listview)

    It's finding it, but its only adding one item to the combo rather than all of them, the code works on another form it basically shows a form when its found, but when adding it to the combo it only shows one entry, but its all defo in the listview.

  14. #14
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,269

    Re: Why does this not work (Finding item in listview)

    Try this

    vb Code:
    1. Dim i As Long, ItemFound As Boolean
    2.     For i = 1 To Form9.ListView1.ListItems.Count
    3.         If Form9.ListView1.ListItems(i).SubItems(2) = Format$(Now, "dd/mm/yyyy") And Form9.ListView1.ListItems(i).SubItems(3) >= "09:00" And Form9.ListView1.ListItems(i).SubItems(3) <= "09:29" Then
    4.               Combo1.Visible = True 'If at least one item is found
    5.              Combo1.Text = Form9.ListView1.ListItems(i).Text
    6.              Combo1.AddItem (Form9.ListView1.ListItems(i).Text)
    7.          End If
    8.     Next i

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