Solved

Programmatically scroll in CListView

Posted on 1997-10-17
3
1,569 Views
Last Modified: 2013-11-20
In a CListView, I can programmatically select an item by this instruction:

ListCtrl.SetItemState(nItem,LVIS_SELECTED|LVIS_FOCUSED,LVIS_SELECTED|LVIS_FOCUSED);

The matter is that if the item is not  currently in  view, the list control doesn't scroll.

Another problem (but reltaed to the first one) is that MFC has a CListCtrl::GetTopIndex() function, but doen't have the corresponding SetTopIndex().

How can I programmatically scroll the events in the ListCtrl associated with a CListView?

0
Comment
Question by:winmeister
3 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1308267
Use CListCtrl::EnsureVisible(). You can make sure an item is visible and whether partially visible is okay or not.
0
 
LVL 1

Author Comment

by:winmeister
ID: 1308268
This is only partially true. How can I get the item as the first in the scroll view? I mean, I'd like to have a function similar to SetTopIndex(nItem).......
0
 
LVL 3

Accepted Solution

by:
Melange earned 50 total points
ID: 1308269
Did you try the Scroll function in CListCtrl?
You can try something like this:

CRect   ItemRect;
CRect   TopRect;

// Let the control do most of the work
ListCtrl.EnsureVisible( Index, FALSE );

CurTop = ListCtrl.GetTopIndex();
if( CurTop == Index )
    return;     // Already finished
ListCtrl.GetItemRect( CurTop, TopRect, LVIR_BOUNDS );
ListCtrl.GetItemRect( Index, ItemRect, LVIR_BOUNDS );
ListCtrl.Scroll( CSize(0, (TopRect.top - ItemRect.top) * ItemRect.Height()) );

Note: I don't believe this will always guarantee that the desired index is always at the top. I don't think the list control will scroll past the end of the list. For example, if the desired index is the last in the control, it won't be displayed anywhere except at the lowest position possible in the window.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Need a Shell script to start a service checking the port 6 52
sameEnds challenge 3 179
FizzBuzz challenge 9 82
wordcount challenge 11 132
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

840 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