Solved

Programmatically scroll in CListView

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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

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