CListbox when adding a new string, scroll to the end

Posted on 2003-03-06
Medium Priority
Last Modified: 2013-11-20

I have a CListBox that I kind of use as a monitor window for output from my program (Visual C++ 6) (with .AddString(string)).
But when the window gets fuller, and a scroll bar appears, it doesn't automatically scroll to the end of the window, to where the last string has been added.
Can this be done in a CListBox or do I need another resource ?
Mind, though, that I would still need to point at a line and get the text selected (now with ..GetText()).
Question by:R-U-Bn
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
LVL 11

Accepted Solution

KurtVon earned 150 total points
ID: 8081554

Expert Comment

ID: 8086119
r u using SetCurSel() function. This function selects a string and scrolls it into view, if necessary. When the new string is selected, the list box removes the highlight from the previously selected string.

int SetCurSel( int n);

Return Value

LB_ERR if an error occurs.


Specifies the zero-based index of the string to be selected. If nSelect is –1, the list box is set to have no selection.

Hope it helps u.

Author Comment

ID: 8089269
I suppose KurtVon is close to what I want (since I don't want the item selected which I didn't mention).

But is there no way it can be done automatically by each addstring by the list control itself, a parameter or so.
I don't like( though I have the feeling I will have to), each time I do an Addstring, also to do a SetTopItem.

I guess I can go into my CListBox creation and add something like the following in the OnDraw or AddString:

But I would prefer to use an MFC defined way if this exist.
LVL 11

Expert Comment

ID: 8089357
I suppose you could just create a descendent class and override AddString, but no, there is no "Add string and scroll to display it" function.

It's a little more work if the list box is sorted, by the way.  In that case the AddString may give a value in the middle of the list, so you need to use GetTopItem to see if you need to scroll up or down, and then do

SetTopItem(max(nItemPos - nNumVisibleItems, 0));

instead if you need to scroll down.

Expert Comment

ID: 8114306
If you wanted to do this and only scroll the box if the scroll position was already at the end (so that the scroll position does not update every time any data gets added, if you were reviewing the data), then you could do something like this:

// Scroll a CListBox to the newly added item ONLY if
// the current scroll position is at the max..
if (ListBox.GetScrollLimit(SB_VERT) ==
  ListBox.SetTopIndex(ListBox.AddString("my text"));
  ListBox.AddString("my text");

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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…
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.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

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