Results 1 to 5 of 5

Thread: [RESOLVED] GoTo List Item and Select It

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Resolved [RESOLVED] GoTo List Item and Select It

    I have a list box with a bunch of dates in it. I also have a MonthView control. I'd like to click a date in the MonthView and have the listbox scroll to the first instance of that date in the list, and select it. I have no idea how to do it so my code isn't much but here it is:

    Code:
    Private Sub mntPickDate_DateClick(ByVal DateClicked As Date)
        Dim intIndex As Integer
        
        For intIndex = 0 To lstHistory.ListCount
            If InStr(1, lstHistory.List(intIndex), Format(DateClicked, "mmmm dd, yyyy")) Then
                Exit For 'change this to whatever will scroll to the item and select it
            End If
        Next intIndex
        MsgBox "No activity was found for the date selected.", vbExclamation + vbOKOnly, "Date Not Found"
        mntPickDate.Visible = False
    End Sub

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: GoTo List Item and Select It

    Would this work for you?
    Code:
    Option Explicit
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const LB_FINDSTRINGEXACT = &H1A2
    
    Private Sub Form_Load()
        Dim i%
        
        For i = 0 To 30
            List1.AddItem Format(DateAdd("d", i, Date), "mm/dd/yyyy")
        Next i
    
    End Sub
    
    Private Sub DTPicker1_Change()
    Dim strText$
    
        strText = Format(DTPicker1.Value, "mm/dd/yyyy")
        List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal strText)
    
    End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: GoTo List Item and Select It

    Quote Originally Posted by RhinoBull
    Would this work for you?
    Code:
    Option Explicit
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
                    (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const LB_FINDSTRINGEXACT = &H1A2
    
    Private Sub Form_Load()
        Dim i%
        
        For i = 0 To 30
            List1.AddItem Format(DateAdd("d", i, Date), "mm/dd/yyyy")
        Next i
    
    End Sub
    
    Private Sub DTPicker1_Change()
    Dim strText$
    
        strText = Format(DTPicker1.Value, "mm/dd/yyyy")
        List1.ListIndex = SendMessage(List1.hwnd, LB_FINDSTRINGEXACT, -1, ByVal strText)
    
    End Sub
    Well I didn't test your code, but I have a question for you.

    I changed it to this:

    Code:
    Private Sub mntPickDate_DateClick(ByVal DateClicked As Date)
        Dim blnFound As Boolean
        Dim intIndex As Integer
        
        blnFound = False
        
        For intIndex = 0 To lstHistory.ListCount
            If InStr(1, lstHistory.List(intIndex), Format(DateClicked, "mmmm dd, yyyy")) Then
                lstHistory.ListIndex = intIndex
                blnFound = True
                Exit For
            End If
        Next intIndex
        
        If blnFound = False Then
            MsgBox "No activity was found for the date selected.", vbExclamation + vbOKOnly, "Date Not Found"
        End If
        
        mntPickDate.Visible = False
    End Sub
    My question to you, is why did you choose to use SendMessage instead of just doing that? I'm not trying to be critical, I just didn't know if there was a specific reason.

    Thanks!

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: GoTo List Item and Select It

    Its a little faster (and I think, a little cleaner)

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2008
    Posts
    294

    Re: GoTo List Item and Select It

    OK Cool. Thanks, Hack.

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