The READIN Family Album
First day of spring! (March 2010)

READIN

Jeremy's journal

Improvement makes straight roads; but the crooked roads without improvement are roads of genius.

— William Blake


(This is a page from my archives)
Front page
More recent posts
Older posts
More posts about:
Programming
Programming Projects
Projects

Archives index
Subscribe to RSS

This page renders best in Firefox (or Safari, or Chrome)

🦋 User Interface feature

When you have a list box that gets items added to it on a continuing basis over the course of a program, it is nice for the list box to scroll downwards as items are added, so the most recent one is always visible. Except when the user is involved in reading some older items -- then this behavior is very annoying. Here is a solution, as implemented in an MFC application -- pretty easy to translate to C/C++* -- other languages, you're on your own:

int GetVisibleCount(CListBox &lb)
{
    CRect rct;
    lb.GetWindowRect(&rct);
    int iHgt = lb.GetItemHeight(0);
    return (rct.bottom - rct.top) / iHgt;
}

    // handler for a custom "Add Item" message
LRESULT CMyDlg::OnAddItem(WPARAM wp, LPARAM lp)
{
    const char *msg   = (const char *) lp;

    int iTop = m_lbRealtimeStatus.GetTopIndex();

    m_lbStatus.AddString(msg);
        // "static" because I am never resizing the 
        // lb -- if you are you will need to calc 
        // this every time.
    static visibleCount = GetVisibleCount(m_lbStatus);
		
    if (iTop == m_lbStatus.GetCount() - 1 - visibleCount)
        m_lbStatus.SetTopIndex(iTop + 1);
    return 0L;
}

So what I am doing is, every time I add an item, I check what the current topmost visible index of the list box is -- if it is not equal to the number of items in the list box less the number of visible items, then I do not scroll. (Note that this calculation doesn't work when there are fewer items in the list box than the max number that will fit on the screen; but that does not matter because there is no need to scroll anyways in that situation.)


* That is to say, C or C++ where you are not using MFC classes.

posted afternoon of Thursday, February 17th, 2005
➳ More posts about Programming
➳ More posts about Programming Projects
➳ More posts about Projects

Respond:

Name:
E-mail:
(will not be displayed)
Link:
Remember info

Drop me a line! or, sign my Guestbook.
    •
Check out Ellen's writing at Patch.com.

Where to go from here...

Friends and Family
Programming
Texts
Music
Woodworking
Comix
Blogs
South Orange
readinsinglepost