Search code examples
c++mfcvisual-studio-2005horizontal-scrollingclistbox

Set horizontal scroll of CListBox row back after resetting content


I got a multiple selection CListBox with horizontal scroll bar enabled and showed correctly. Problem is, that when I use function

lst.ResetContent() and fill it back, I can't find way to scroll text in the rows back to the same position. I tried to use

lst.SetScrollPos(SB_HORZ, horizScroll, TRUE); , where horizScroll = lst.GetScrollPos(SB_HORZ); This works correctly on scroll bar itself, but

text in the row stays not scrolled (manual scrolling functions OK).

Structure of my program is:

CListBox lst;
int horizScroll;

/*Periodically doing code bellow*/
//Get current scroll position
horizScroll = lst.GetScrollPos(SB_HORZ);
//Reset current content
lst.ResetContent();
//Add item into CListBox (UNICODE in my application)
lst.AddString(L"Some longer text then width of CListBox");
//Calculate horizontal extent and set it through  
lst.SetHorizontalExtent(calculatedWidth); 
//Try to scroll text (scrolls only scroll bar, not text itself)
lst.SetScrollPos(SB_HORZ, horizScroll, TRUE);
UpdateData(FALSE);

Thanks in advance!

EDIT: As "rrirower" answered correctly, lst.PostMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, 250), 0); message does the job. Scroll position from horizScroll works perfectly. I suggest posting this message twice, because if you do it only once, text is re-scrolled visually from beginning to the wanted position. When you post it twice, text visually stays at the correct position and scroll bar just quickly comes to the right place.


Solution

  • If I understand you correctly, you're trying to scroll the text in the list box horizontally using the program code. If you use Spy++, you'll see that when you manually scroll, using the mouse, a series of WM_HSCROLL messages is posted to the list box control. You can accomplish the same thing by doing this...

    lst.PostMessage(WM_HSCROLL, MAKEWPARAM(SB_THUMBPOSITION, 250), 0);
    

    You need to calculate the position (I used 250 above), but, the above code should move the text and the scroll bar horizontally.