Click to See Complete Forum and Search --> : List Control


babu
May 8th, 1999, 01:09 AM
Hi,
In the list view control, if you delete an item how do set back the focus onto any other item in the list.
thanx

giri

Stu
May 13th, 1999, 11:12 AM
The code is simple, but you must handle 2 cases:

* the deleted item being the first in the list - you then select the next one.

* the deleted item is somewehere else - selecting the previous item is probably best.

For the code fragment, assume m_nSelectedItem was the index of the deleted item.

...
if(m_nSelectedItem == 0)
{
// Select the next item in the list
LVITEM lvItem;
lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;

m_ListCtrl.SetFocus();
m_ListCtrl.SetItemState(m_nSelectedItem, &lvItem);
}
else
{
// Select the previous item in the list
LVITEM lvItem;
lvItem.state = LVIS_FOCUSED | LVIS_SELECTED;
lvItem.stateMask = LVIS_FOCUSED | LVIS_SELECTED;

m_ListCtrl.SetFocus();
m_ListCtrl.SetItemState(m_nSelectedItem - 1, &lvItem);
}

NB: _you_ may not need to use SetFocus()

Stu

Jonathan
May 21st, 1999, 01:28 AM
I have a search function in my application which is looking from the CListCtrl. I have done the SetItemState and managed to highlight and focus on the item.
My problem is how can I make the CListCtrl to scroll to the focused item's page? Thanks.

Stu
May 21st, 1999, 05:18 AM
This surprises me - I assume you don't have access to VC help or even the entire MSDN library at http://msdn.microsoft.com/library/default.htm ?! ;-)

Try CListCtrl::EnsureVisible()