• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2260
  • Last Modified:

CListbox when adding a new string, scroll to the end

Hi,

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()).
0
R-U-Bn
Asked:
R-U-Bn
1 Solution
 
KurtVonCommented:
pListBox->SetTopItem(pListBox->AddString(strText));
0
 
manirupCommented:
hello
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.

CListBox::SetCurSel
int SetCurSel( int n);

Return Value

LB_ERR if an error occurs.

Parameter

n
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.
Bye
Manirup
 
0
 
R-U-BnAuthor Commented:
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:
       this.SetTopItem(9999);
or
       this.SetTopItem(GetCount);

But I would prefer to use an MFC defined way if this exist.
0
 
KurtVonCommented:
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.
0
 
jkramer_cvCommented:
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.GetScrollPos(SB_VERT)maxscroll)
  ListBox.SetTopIndex(ListBox.AddString("my text"));
else
  ListBox.AddString("my text");
///////////////////////////
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now