🦋 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
|