Results 1 to 23 of 23

Thread: Changing Code?

  1. #1

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362

    Question Changing Code?

    Does anybody know how to adapt this Code to a "listview box"?



    VB Code:
    1. Private Sub Text1_Change()
    2. Dim i As Integer
    3. For i = 0 To List1.ListCount - 1
    4.     If InStr(1, Left(List1.List(i), Len(Text1.Text)), Text1.Text, vbTextCompare) Then List1.Selected(i) = True: Exit For
    5. Next
    6.  
    7. End Sub
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  2. #2
    Frenzied Member Rick Bull's Avatar
    Join Date
    Apr 2002
    Location
    England
    Posts
    1,444
    I haven't tested this, so it could be a little wrong, but try this:

    VB Code:
    1. Private Sub Text1_Change()
    2. Dim i As Integer
    3. For i = 0 To ListView1.ListItems.Count - 1
    4.     If InStr(1, Left(ListView.ListItems(i).Caption, Len(Text1.Text)), Text1.Text, vbTextCompare) Then ListView.ListItems(i).Selected = True: Exit For
    5. Next
    6.  
    7. End Sub

  3. #3

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362
    Dude, that's almost exactly what I tryed.

    VB Code:
    1. Dim i As Integer
    2. For i = 1 To ListView1.ListItems.Count
    3.     If InStr(1, Left(ListView1.ListItems(i).Text, Len(Text1.Text)), Text1.Text, vbTextCompare) Then ListView1.ListItems(i).Selected = True: Exit For
    4. Next
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  4. #4
    PowerPoster
    Join Date
    Aug 2001
    Location
    new jersey
    Posts
    2,904
    Well, "new to vb6" I haven't looked at the specifics of RickBull's post, but your response is stunning. "Almost exactly the same" counts in horseshoes and hand grenades, but not in programming.

    If you don't understand that even a single comma can totally change what a program does, you should probably look for another line of work.

  5. #5
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    maybe if you looked at their codes, phinds, you would notice that the only difference is Caption and Text, and i dont think that a listview would have a caption property ...... dont criticize people

    also the 0 and 1 difference but eh.......
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  6. #6

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362
    ListView doesn't have a Caption property and if I use 0 I get an invalid index error, so I used 1 and that got rid of the error.

    The only this is that when I type in some text Nothing happens.
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  7. #7
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    VB Code:
    1. Private Sub Text1_Change()
    2.    Dim i As Integer
    3.    For i = 1 To Listview1.ListCount
    4.       If InStr(1, Left(Listview1.List(i), Len(Text1.Text)), Text1.Text, Listview1.ListItems(i)) Then
    5.          Listview1.ListItems(i).Selected = True
    6.          Exit For
    7.    Next
    8. End Sub

    That should do it.

    Just remember that for the common controls, all arrays are indexed from 1, not 0, as with the standard controls.

    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  8. #8
    PowerPoster rjlohan's Avatar
    Join Date
    Sep 2001
    Location
    Sydney, Australia
    Posts
    3,205
    Originally posted by phinds
    Well, "new to vb6" I haven't looked at the specifics of RickBull's post, but your response is stunning. "Almost exactly the same" counts in horseshoes and hand grenades, but not in programming.

    If you don't understand that even a single comma can totally change what a program does, you should probably look for another line of work.
    That sort of comment is pretty unnecessary.
    -----------------------------------------
    -RJ
    [email protected]
    -----------------------------------------

  9. #9

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362
    [QUOTE]Originally posted by rjlohan
    [B]
    VB Code:
    1. Private Sub Text1_Change()
    2.    Dim i As Integer
    3.    For i = 1 To Listview1.ListCount
    4.       If InStr(1, Left(Listview1.List(i), Len(Text1.Text)), Text1.Text, Listview1.ListItems(i)) Then
    5.          Listview1.ListItems(i).Selected = True
    6.          Exit For
    7.    Next
    8. End Sub

    ListView doesn't have a list or listcount property. Will this work?
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  10. #10
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    listview1.listitems.count

    or

    listview1.listitems.listcount

    its one of them. cant remember
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  11. #11

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362
    Originally posted by rjlohan
    VB Code:
    1. Private Sub Text1_Change()
    2.    Dim i As Integer
    3.    For i = 1 To Listview1.ListCount
    4.       If InStr(1, Left(Listview1.List(i), Len(Text1.Text)), Text1.Text, Listview1.ListItems(i)) Then
    5.          Listview1.ListItems(i).Selected = True
    6.          Exit For
    7.    Next
    8. End Sub
    When I try it your way and change it to the way BuggyProgrammer showed I get an error "Next without For" and when I try

    VB Code:
    1. Dim i As Integer
    2.    For i = 1 To ListView1.ListItems.Count
    3.       If InStr(1, Left(ListView1.ListItems(i), Len(Text1.Text)), Text1.Text, ListView1.ListItems(i)) Then ListView1.ListItems(i).Selected = True: Exit For
    4.    Next

    I get a "Type mismatch" error
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  12. #12
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    he forgot the End If

    VB Code:
    1. Private Sub Text1_Change()
    2.    Dim i As Integer
    3.    For i = 1 To Listview1.ListCount
    4.       If InStr(1, Left(Listview1.List(i), Len(Text1.Text)), Text1.Text, Listview1.ListItems(i)) Then
    5.          Listview1.ListItems(i).Selected = True
    6.          Exit For
    7.        End If
    8.    Next
    9. End Sub
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  13. #13

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362
    I still get a type mismatch error

    VB Code:
    1. For i = 1 To ListView1.ListItems.Count
    2.       If InStr(1, Left(ListView1.ListItems(i), Len(Text1.Text)), Text1.Text, ListView1.ListItems(i)) Then
    3.          ListView1.ListItems(i).Selected = True
    4.          Exit For
    5.        End If
    6.    Next
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  14. #14

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362
    Ok Let's get out stories straight...

    Some of this isn't nedded. I was working on a list box because I was told to use one by someone else.

    I thought ListView was better when I found out that ListView Used keys.

    Let's work one either the list box or the listview.
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  15. #15
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    nothing to do till soccer(or football ) comes on so ill test and fix it for you.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  16. #16
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    man whoever typed this before was really bad at this stuff

    VB Code:
    1. Dim i As Integer
    2.    For i = 1 To ListView1.ListItems.Count
    3.       If InStr(ListView1.ListItems(i), Text1.Text) > 0 Then ListView1.ListItems(i).Selected = True: Exit For
    4.    Next


    the problem was in the Instr, which for some funny reason did something so screwy it cannot be described, other than the person who wrote this code.


    and you also dont need the left() part, unless you could tell me why
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  17. #17

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362
    MidgetsBro originly wrote:

    VB Code:
    1. Private Sub Text1_Change()
    2. Dim i As Integer
    3. For i = 0 To List1.ListCount - 1
    4.     If InStr(1, Left(List1.List(i), Len(Text1.Text)), Text1.Text, vbTextCompare) Then List1.Selected(i) = True: Exit For
    5. Next
    6.  
    7. End Sub

    for a list box control

    See this Thread
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  18. #18

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362
    I don't care about the code, I just want to compare the text in Text1 to the text in the ListView Control.
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  19. #19
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    well does my code wrok for you?
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  20. #20

    Thread Starter
    Hyperactive Member New to VB 6's Avatar
    Join Date
    Apr 2002
    Posts
    362
    Yes and No

    Yes it works as far as no error msg.
    No it doesn't retrieve the listview item...
    [VBCODE]
    Option Explicit
    Dim XXX As Porn
    Dim Wife As Nag

    Private Sub *****_Resize()
    On Error Resume Next
    Get Viagra
    End Sub
    [/VBCODE]

  21. #21
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    did you try setting focus to the listview? because it wont seem to highlight anything if it doesnt.
    Remember, if someone's post was not helpful, you can always rate their post negatively .

  22. #22
    Hyperactive Member
    Join Date
    May 2002
    Location
    Chicago
    Posts
    271
    This code will compare the text in Text1 to the ListView and select the
    first listview item that matches. Place a textbox (Text1), a command
    button (Command1) and a listview (ListView1) on a form and paste
    this code.
    Code:
    Private Sub Form_Load()
    '***************************************
      'fill your list here
    '***************************************
      
      Text1.Text = ""
      Text1.TabIndex = 0
      Command1.Default = True
    End Sub
    
    Private Sub Command1_Click()
      Dim liFound As ListItem   ' FoundItem variable.
      
      Set liFound = ListView1.FindItem(Text1, , , lvwPartial)
      
      If liFound Is Nothing Then 'no match
        MsgBox "No match found"
        Exit Sub
      Else 'at least a partial match
        liFound.EnsureVisible ' Scroll ListView to show found ListItem.
        Set ListView1.SelectedItem = liFound 'select the ListItem
        'or you could use this line
        'liFound.Selected = True ' Select the ListItem.
        ListView1.SetFocus 'keep item selected
      End If
    End Sub
    
    Private Sub Text1_GotFocus()
      If Text1 = "" Then Exit Sub
      Text1.SelStart = 0
      Text1.SelLength = Len(Text1)
    End Sub
    Sometimes what you're looking for is exactly where you left it.

  23. #23
    Hyperactive Member
    Join Date
    May 2002
    Location
    Chicago
    Posts
    271
    Note: in the above example, a match will only occur if the text matches
    the upper most level in the list. To search child levels you must change
    Code:
    object.FindItem (string, [value], index, match)
    [value] to the items level (0 is default). In code you could also cycle through
    each level until a match occurs by changing [value] in the finditem string.
    Sometimes what you're looking for is exactly where you left it.

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