CListCtrl LVN_ITEMCHANGING
hi @all
I want to handle the LVN_ITEMCHANGING notification for one of my CListCtrl (Report Style) to prevent change of item focus. According to MSDN, returning *pResult = TRUE from the LVN_ITEMCHANGING notification handler should prevent the change.
Problem: When I try to change the item focus by mouse and display a (Afx)MessageBox(...) within the LVN_ITEMCHANGING handler function, the message is received a 2nd time as soon as the mouse is moved.
This does not occur when either (a) I change the focus item with the cursor-keys on the keyboard or (b) I don't display a message box.
You can breakpoint in the notification handler and see, that the pNMListView data is excatly the same for the doubled message.
This is the relevant code:
Code:
void CListCtrl_LVN_ITEMCHANGINGDlg::OnItemChangingList1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// check for change of LVIS_FOCUSED state
if ( (pNMListView->uChanged == LVIF_STATE) && (pNMListView->uOldState == LVIS_FOCUSED) )
{
// display message box
AfxMessageBox("Item change not allowed.");
// disallow itemchange
*pResult = TRUE;
}
else
{
// allow itemchange
*pResult = FALSE;
}
}
void CListCtrl_LVN_ITEMCHANGINGDlg::OnItemChangingList2(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// check for change of LVIS_FOCUSED state
if ( (pNMListView->uChanged == LVIF_STATE) && (pNMListView->uOldState == LVIS_FOCUSED) )
{
// play sound
MessageBeep(MB_ICONEXCLAMATION);
// disallow itemchange
*pResult = TRUE;
}
else
{
// allow itemchange
*pResult = FALSE;
}
}
I've also built a small demo project (dialog based) you may want to download and have a look into.
Any suggestions are greatly appreciated! Thank you!
Oliver.
Re: CListCtrl LVN_ITEMCHANGING
I am trying to do the same thing, 9 years after you. Anyways, I noticed that the second message box appears after I have moved my mouse. The only way to get that to happen is to hit Enter on your keyboard when the message box appears. When I then move my mouse over a different item than it is already on, I get the second message box.
Oliver, did you ever resolve this?
Re: CListCtrl LVN_ITEMCHANGING
Small correction: I noticed the second message box everytime that I use my mouse to click on a different item. I only noticed that it happens when I move my mouse over another item, if I use Enter to clear the message box.
Re: CListCtrl LVN_ITEMCHANGING
I found what appears to be you asking the same question again in 2005. There it appears that you figured it out. I used your possible solution and built on it because it was losing the selection. See my example.
void CCommentsTabDlg::OnItemchangingCmntsCommentList(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
if ( pNMListView->uChanged == LVIF_STATE &&
( pNMListView->uOldState == LVIS_FOCUSED ||
pNMListView->uOldState == LVIS_SELECTED ) &&
( m_nMode == MODE_ADD || m_nMode == MODE_UPDATE ) ) {
// Only show message once to prevent the message when focus and selection change.
if ( pNMListView->uOldState == LVIS_FOCUSED ) {
if ( m_nMode == MODE_ADD ) {
m_sUpdateAddMessage = "You cannot select another comment while adding.\n\nYou must finish adding first.";
}
else if ( m_nMode == MODE_UPDATE ) {
m_sUpdateAddMessage = "You cannot select another comment while updating.\n\nYou must finish updating first.";
}
PostMessage(WM_UPDATE_ADD_MESSAGE);
}
// Do not allow ItemChange
*pResult = -1;
}
else {
*pResult = 0;
}
}
2005 Thread: http://forums.codeguru.com/archive/i...t-344941.html?
Re: CListCtrl LVN_ITEMCHANGING
LVN_ITEMCHANGING cannot be used to prevent actual change of focus.
This message is merely posted so you can prevent the status change from being applied to the item. (i.e. the small focus rectangle around an item.
If what you seek to do is actual focus change prevention, then you have a much much more complex task ahead of you and even one that will violate many core principles in Windows. Your users may not end up being very happy depending on how strict you apply such rules.
Re: CListCtrl LVN_ITEMCHANGING
Personally, I am trying to prevent the selected item from being changed. The code does exactly what I want it to do. I did have to change one small piece:
FROM:
if ( pNMListView->uOldState == LVIS_FOCUSED ) {
TO:
if ( pNMListView->uOldState == LVIS_SELECTED ) {
Re: CListCtrl LVN_ITEMCHANGING
I replied in your other thread, basically you should not change any selection or focus in the handler code for LVN_ITEMCHANGING. Instead use SetTimer() to make the changes after the handler has completed.
Re: CListCtrl LVN_ITEMCHANGING
Quote:
Originally Posted by
zerver
Nobody cares how it works as long as it works
I completely agree and my code works for my situation.