?
Solved

Programmatically scroll in CListView

Posted on 1997-10-17
3
Medium Priority
?
1,596 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
[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
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 100 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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 tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

764 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