Searching List1
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: Searching List1

  1. #1
    Join Date
    Jul 2008
    Posts
    45

    Searching List1

    Hi again,
    I have a list box which display the following:

    0001 - First Game
    0002 - Second Game
    0004 - Thrid Game 'Sometimes theres one missing eg 0002 - 0004

    What i am trying to do is a search so that the user can type 0 into a box named str_search and then click search and it will jump to the list with a 0 in it, how would i go about doing this.

    Also because there is three 0 in the first three, i only want it to jump to each item in the list once.

    Thanks
    Chris

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,982

    Re: Searching List1

    Try this:

    Code:
    Option Explicit
    
    Private Declare Function SendMessageByNum Lib "user32" _
    Alias "SendMessageA" (ByVal hwnd As Long, ByVal _
    wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Const LB_SETHORIZONTALEXTENT = &H194
    
    Private Sub Form_Load()
      List1.AddItem "this is a test.  this is only a test."
      SendMessageByNum List1.hwnd, LB_SETHORIZONTALEXTENT, 250, 0
    End Sub
    
    Sub lstAddHScroll(lst As ListBox)
        Dim a%, m%
        ' depends on the scalewidth
        ' if scalemode is Twips then Divide M by 15 to get Pixels
        For a = 0 To lst.ListCount - 1
            If Me.TextWidth(lst.List(a)) > m Then m = Me.TextWidth(lst.List(a))
        Next
        SendMessageByNum lst.hwnd, LB_SETHORIZONTALEXTENT, m / 15, 0
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,091

    Re: Searching List1

    Quote Originally Posted by Chrispy360 View Post
    Hi again,
    I have a list box which display the following:

    0001 - First Game
    0002 - Second Game
    0004 - Thrid Game 'Sometimes theres one missing eg 0002 - 0004

    What i am trying to do is a search so that the user can type 0 into a box named str_search and then click search and it will jump to the list with a 0 in it, how would i go about doing this.

    Also because there is three 0 in the first three, i only want it to jump to each item in the list once.

    Thanks
    Chris
    IMHO, you'd save yourself a lot of trouble by searching for the precence of "1" or "2" or "4" etc. in the list - that would ensure that you get only one result at a time, instead of adding more logic to compensate for the 3 zero's

  4. #4
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    303

    Re: Searching List1

    Could you elaborate more? All your data has zero so it will jump on all of them.

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,722

    Re: Searching List1

    I assume you want to jump to the first matching item. So if you type in more letters the more precise will be your jump.

    In the below sample the ListView is call lstSoundFiles. (I just took it from another sample I did recently).

    You type letters in a TextBox named txt_search, click on a button named btnSearch and the jump will take place.

    Code:
    Private Sub btnSearch_Click()
      Dim i%
      For i = 0 To lstSoundFiles.ListCount - 1
        If InStr(lstSoundFiles.List(i), txt_search.Text) = 1 Then
           lstSoundFiles.ListIndex = i
           Exit Sub
        End If
      Next
      MsgBox "notfound", vbOKOnly
    End Sub

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center