Solved

Programmatically scroll in CListView

Posted on 1997-10-17
3
1,528 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now