Ryan Chong
asked on
Make Listview scroll down to selected item
Hi guys,
Assume that i have a listview with thousand of items..
What i want to do is let the Listview control auto scroll down to the selected item. (Make the listview scroll down itself so i can 'see' the selected item.)
Here is the code:
Private Const WM_VSCROLL = &H115
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 Sub Command1_Click()
Dim i As Long
i = SendMessage(ListView1.hwnd , WM_VSCROLL, 0, ByVal CLng(Text1.Text))
End Sub
Private Sub Form_Load()
For i = 1 To 250
ListView1.ListItems.Add i, , i
ListView1.ListItems(i).Sub Items(1) = "Subitem " & i
Next i
Text1.Text = "25"
Randomize
Dim j As Long
j = Int(Rnd * ListView1.ListItems.Count) + 1
ListView1.ListItems(j).Sel ected = True
End Sub
What is less in this code above? What params should i use in SendMessage to do the tricks?
Reference: http://www.mvps.org/vbnet/code/subclass/lvscrollbars.htm
Any comments, resources or links? Thanks.
Assume that i have a listview with thousand of items..
What i want to do is let the Listview control auto scroll down to the selected item. (Make the listview scroll down itself so i can 'see' the selected item.)
Here is the code:
Private Const WM_VSCROLL = &H115
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 Sub Command1_Click()
Dim i As Long
i = SendMessage(ListView1.hwnd
End Sub
Private Sub Form_Load()
For i = 1 To 250
ListView1.ListItems.Add i, , i
ListView1.ListItems(i).Sub
Next i
Text1.Text = "25"
Randomize
Dim j As Long
j = Int(Rnd * ListView1.ListItems.Count)
ListView1.ListItems(j).Sel
End Sub
What is less in this code above? What params should i use in SendMessage to do the tricks?
Reference: http://www.mvps.org/vbnet/code/subclass/lvscrollbars.htm
Any comments, resources or links? Thanks.
try this:
Listview1.TopIndex = ListView1.SelectedItem.Ind ex
hope this helps!
Listview1.TopIndex = ListView1.SelectedItem.Ind
hope this helps!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hi JonFish85,
TopIndex property only available on List box control, NOT listview control, i think maybe you missread my question.
To vblogic,
You code works fine.
TopIndex property only available on List box control, NOT listview control, i think maybe you missread my question.
To vblogic,
You code works fine.
I knew it worked in listbox, but I wasnt sure if listview had the same property, but I thought it worth a shot... sorry it didnt work :-/
ASKER
Hi JonFish85,
Unfortunately that listview don't have this property. It's ok : )
The ensurevisible method works fine for me, but if somebody can provides me a way to do same effect using API, points will be added.
Thanks Jon and vblogic.
Unfortunately that listview don't have this property. It's ok : )
The ensurevisible method works fine for me, but if somebody can provides me a way to do same effect using API, points will be added.
Thanks Jon and vblogic.
Why use complicated API when ListView has built-in capability to achieve this effect?
ASKER
Hi vblogic,
Don't worry, the points are yours.
I just want to learn more on doing same thing using API, more fun, isn't it?! : )
Don't worry, the points are yours.
I just want to learn more on doing same thing using API, more fun, isn't it?! : )
I think its more headache! j/k
But I dont know...your code looks ok to me, except that I think you may need to get handle to the scrollbar of the listview, and not the listview itself.
But I dont know...your code looks ok to me, except that I think you may need to get handle to the scrollbar of the listview, and not the listview itself.
hearing
ASKER
Can anyone show me the way to do this in API? or i will close this question.
Since that control is created by API, i think that this site could give you a great clue:
http://www.mvps.org/btmtz/enumdeskvb/
http://www.mvps.org/btmtz/vbexplorer/
http://www.mvps.org/btmtz/enumdeskvb/
http://www.mvps.org/btmtz/vbexplorer/
Maybe here (sorry, i have no time to test them by myself):
http://www.mvps.org/vbnet/index.html?code/subclass/lvscrollbars.htm
Hope it helps
http://www.mvps.org/vbnet/index.html?code/subclass/lvscrollbars.htm
Hope it helps
ASKER
Sorry for late reply, i will check them and reply soon..
Thanks Richie.
Thanks Richie.
You are welcome anytime ;)
ASKER
Hi vblogic,
Thanks for your help. But my problem is NOT solved yet..
To Richie,
Thanks to provide me some useful links, but still can not automatically scroll down the listview to selected item. Maybe no luck this time.
Anyway, thanks all to provide valuable comments.
Thanks for your help. But my problem is NOT solved yet..
To Richie,
Thanks to provide me some useful links, but still can not automatically scroll down the listview to selected item. Maybe no luck this time.
Anyway, thanks all to provide valuable comments.
Try this
ListView1.HideSelection = False
ListView1.ListItems(x).Sel ected = True
ListView1.ListItems(x+5).E nsureVisib le
Mitch
ListView1.HideSelection = False
ListView1.ListItems(x).Sel
ListView1.ListItems(x+5).E
Mitch
ASKER
Welcome to EE, mbalsam.
Thanks for the comment but this question is already PAQ, so it's not worth to post any comment in this question anymore.
Instead, try help other people to solve their problem in:
https://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/
cheers :)
Thanks for the comment but this question is already PAQ, so it's not worth to post any comment in this question anymore.
Instead, try help other people to solve their problem in:
https://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/
cheers :)
ASKER
I add a for loop in order to do the tricks. As get the maximum items can be displayed , and divide it with the total items count:
If i > 21 Then
Dim k As Integer
For k = 1 To Int(i / 21)
j = SendMessage(LVIns.hwnd, WM_VSCROLL, 3, 0)
Next k
End If
Any alternative?