Home » Category » Microsoft Visual C & C++

Microsoft Visual C & C++: List Control

202| Tue, 05 Feb 2008 23:39:00 GMT| babu| Comments (3)
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

Keywords & Tags: list, control, microsoft, visual c++, vc

URL: http://www.7prog.com/visual-c-c++/189410/
 
«« Prev - Next »» 3 helpful answers below.
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

stu | Sat, 10 Nov 2007 04:06:00 GMT |

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.

jonathan | Sat, 10 Nov 2007 04:07:00 GMT |

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()

stu | Sat, 10 Nov 2007 04:08:00 GMT |

Microsoft Visual C & C++ Hot Answers

Microsoft Visual C & C++ New questions

Microsoft Visual C & C++ Related Categories