Solved

Make Listview scroll down to selected item

Posted on 2001-09-02
20
1,439 Views
Last Modified: 2012-08-14
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).SubItems(1) = "Subitem " & i
    Next i
    Text1.Text = "25"
    Randomize
    Dim j As Long
    j = Int(Rnd * ListView1.ListItems.Count) + 1
    ListView1.ListItems(j).Selected = 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.
0
Comment
Question by:Ryan Chong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
  • 3
  • +2
20 Comments
 
LVL 52

Author Comment

by:Ryan Chong
ID: 6449701
Hi guys,

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?
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6449709
try this:

Listview1.TopIndex = ListView1.SelectedItem.Index

hope this helps!
0
 
LVL 1

Accepted Solution

by:
vblogic earned 43 total points
ID: 6449712
Listview.ListItems(i).EnsureVisible
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 52

Author Comment

by:Ryan Chong
ID: 6449754
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.
0
 
LVL 6

Expert Comment

by:JonFish85
ID: 6449760
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 :-/
0
 
LVL 52

Author Comment

by:Ryan Chong
ID: 6449769
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.
0
 
LVL 1

Expert Comment

by:vblogic
ID: 6449770
Why use complicated API when ListView has built-in capability to achieve this effect?
0
 
LVL 52

Author Comment

by:Ryan Chong
ID: 6449780
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?! : )
0
 
LVL 1

Expert Comment

by:vblogic
ID: 6449818
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.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6451660
hearing
0
 
LVL 52

Author Comment

by:Ryan Chong
ID: 6463346
Can anyone show me the way to do this in API? or i will close this question.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6463888
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/
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6463890
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6463912
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
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6463975
0
 
LVL 52

Author Comment

by:Ryan Chong
ID: 6469381
Sorry for late reply, i will check them and reply soon..

Thanks Richie.
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6469526
You are welcome anytime ;)
0
 
LVL 52

Author Comment

by:Ryan Chong
ID: 6475587
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.

0
 

Expert Comment

by:mbalsam
ID: 7467019
Try this
    ListView1.HideSelection = False
    ListView1.ListItems(x).Selected = True
    ListView1.ListItems(x+5).EnsureVisible

Mitch
0
 
LVL 52

Author Comment

by:Ryan Chong
ID: 7467359
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:
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/

cheers :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question