[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
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
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!
Re: GoTo List Item and Select It
Its a little faster (and I think, a little cleaner)
Re: GoTo List Item and Select It