none
Problem facing with CListBox with Itemdata RRS feed

  • Question

  • Hi,

    I am working on a Dialog based application with MFC.

    I have a list box on my Dialog. and the each items of the list are attached with some item data using SetItemDataPtr().

    As I am using some structure to attach with each item of the list, so I am using SetItemdataPtr() instead of SetItemdata().

    Now the requirement is such that I need to move the items of the list up and down, and the item data should be intact with the item .

    I tried the solution provided n the following link:

    http://stackoverflow.com/questions/12068442/move-an-item-up-or-down-in-a-list-box

    but here they are removing the item and then adding a new item on the new posion. But this way I am loosing the Itemdata when I delete the Item.

    Please guide me if there is any way so that I can move the items up and down along with the item data.

    Thanks in Advance.


    Thanks & Regards, Mayank Agarwal

    Friday, December 16, 2016 6:09 AM

Answers

  • In order to retain the ItemData you need to handle the WM_DELETEITEM message.  This message will pass a pointer to a DELETEITEMSTRUCT that contains the ItemData pointer from the deleted item.

    Alternatively, you can use CListBox::GetItemData to retrieve the pointer for reuse before you delete the item.

    You can use this pointer to reattach the ItemData to the new Listbox item that you create during the re-positioning process.


    • Edited by RLWA32 Friday, December 16, 2016 11:42 AM
    • Proposed as answer by Baron Bi Friday, December 23, 2016 7:26 AM
    • Marked as answer by MayankAg Friday, December 23, 2016 11:44 AM
    Friday, December 16, 2016 11:03 AM

All replies

  • Hi MayankAg,

    thanks for posting here.

    >>but here they are removing the item and then adding a new item on the new posion. But this way I am loosing the Itemdata when I delete the Item.

    When you use CListBox::SetItemDataPtr, this pointer remains valid for the life of the list box, even though the item's relative position within the list box might change as items are added or removed. Hence, the item's index within the box can change, but the pointer remains reliable. You could recall this after you remove and add to the right position.

    >>Please guide me if there is any way so that I can move the items up and down along with the item data.

    With CListBox class, maybe remove and add operation is the only way. But there are three MFC classes that are involved in a Drag and Drop operation. These classes are COleDataSource, COleDropSource, and COleDropTarget.

    Here is a sample about how to use OLE to implement Drag and Drop Listbox Items.

    https://www.codeproject.com/Articles/12342/Drag-and-Drop-Listbox-Items-using-OLE

    Hope this could be help of you.

    Best Regards,

    Sera Yu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Baron Bi Friday, December 23, 2016 7:26 AM
    Friday, December 16, 2016 7:28 AM
  • In order to retain the ItemData you need to handle the WM_DELETEITEM message.  This message will pass a pointer to a DELETEITEMSTRUCT that contains the ItemData pointer from the deleted item.

    Alternatively, you can use CListBox::GetItemData to retrieve the pointer for reuse before you delete the item.

    You can use this pointer to reattach the ItemData to the new Listbox item that you create during the re-positioning process.


    • Edited by RLWA32 Friday, December 16, 2016 11:42 AM
    • Proposed as answer by Baron Bi Friday, December 23, 2016 7:26 AM
    • Marked as answer by MayankAg Friday, December 23, 2016 11:44 AM
    Friday, December 16, 2016 11:03 AM