CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 2 of 2

Thread: CListCtrl selection changing handling

  1. #1
    Join Date
    Jun 2008

    CListCtrl selection changing handling

    Hello everyone,

    I have a problem handling the selection change message for my CListCtrl. I subscribe to the LVN_ITEMCHANGING message in my dialog, perform some checks which determine whether the item should be allowed to change, and return TRUE if the selected item should not be the new selection, and FALSE if it can be.

    The problem I am having is that this introduces heap corruption. I have tested the code by calling the handler with arguments which would normally be expected by the handler from the windows message (as well as testing the code piece by piece outside of the message handler), but then I have no heap corruption, therefore I believe it is not my processing code causing the problem (hopefully). For some reason it looks like during regular message handling the heap is corrupted someway, so could anyone confirm for me that I am going about this the right way? The program crashes with a corrupted heap after several selection changes are processed. The following is some code which demonstrates what I am doing, and included comments ask some of the questions I have.

    static bool isItemSelected(UINT state)
        if((state & LVIS_SELECTED) == LVIS_SELECTED)
             return true;
        return false;
    void MyPage::OnItemChanging(NMHDR* pNMHDR, LRESULT* pResult)
        LPNMLISTVIEW item = reinterpret_cast<LPNMLISTVIEW>(pNMHDR);
        // Is this the right way to check that this message is actually changing the item now?
        // The reason I ask is that for every single change attempt, multiple message are sent,
        // and I assume not all of them need to be handled?
        if (!isItemSelected(item->uOldState) && isItemSelected(item->uNewState))
              ... // Do my object initialization...
              // Check if objects associated with the selection initialized properly
              // which dictates if the item can be the new selection.
                   *pResult = TRUE; // No success, prevent the list from changing selection.
        // For anything else set the result to FALSE.
        // Should the result be FALSE for all messages sent for each selection change,
        // even if during one call I return TRUE as in above part of the code?
        *pResult = FALSE;
    I would appreciate any help I can get with this.
    Last edited by mutated; December 4th, 2009 at 12:49 PM.

  2. #2
    Join Date
    May 1999

    Re: CListCtrl selection changing handling

    Let me start from setting a common ground:
    LVN_ITEMCHANGING is not a message, it is a notification code reflected from a control.
    You do not subscribe to it, you simply handle it in a WM_NOTIFY message for LVN_ITEMCHANGING notification code.
    Than you tell a child control, OK I have handled it and I am returning TRUE or you handle it I am returning FALSE.

    You will receive multiple notifications upon state change, since item that is losing selection will also change other state (highlight).

    It looks like your code preventing selection change is OK; however you can simplify it:
     void CTsListCtrlView::OnLvnItemchanging(NMHDR *pNMHDR, LRESULT *pResult)
    	*pResult = 0;
    	if((pNMLV->uNewState & LVIS_SELECTED))
    		// do what you want to do
    		*pResult = 1;
    You have to prevent gaining selection; the rest is done by a control. You also have to remember, that control sends notification when selection changes as a result of a call to set state.

    The following part is uncertain though:
    Quote Originally Posted by mutated View Post
    ... // Do my object initialization...
    // Check if objects associated with the selection initialized properly
    // which dictates if the item can be the new selection.
    This is a part responsible for a problem you have.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)