Solved

Programmatically scroll in CListView

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

777 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