3

I've just added one of the new (MFC Feature Pack) CVSListBox controls to a project. The list of items in the control is tracked by some other objects in my application, so I need to take lots of notifications from the list-box when anything changes so that I can update other stuff. For those that don't know the control, there is a button bar which offers basic add/delete/reorder functionality.

The CVSListBox control offers overridable virtual functions for things like adding or renaming items, and changing their order - all this works nicely.

However, for deleting items, the only override is OnBeforeRemoveItem, which is called BEFORE an item is removed, and from which one has to return TRUE/FALSE to permit the remove. Once the remove has occurred, there's no specific notification.

What's the best way to get notification AFTER a remove?

Obviously it's possible to hack something horrible here, in that there will be a selection-changed event after a remove, and it would be possible to hold a flag from the before-remove to say that the next selection-changed is special. But I feel like I'm missing something cleaner and more obvious. Any suggestions?

Will Dean
  • 39,055
  • 11
  • 90
  • 118

2 Answers2

1

Assuming that the item will truly be removed every time, you could either:

  • Do the handling in the OnBeforeRemoveItem override as if the item was already removed
  • Raise your own OnAfterItemRemoved event
  • See if you can get a handle on the underlying list control (whatever it may be) and hook one of its events
Aidan Ryan
  • 11,389
  • 13
  • 54
  • 86
0

Try something like this:

class my_lbox : public CVSListBox
{
    protected:

        BOOL OnBeforeRemoveItem(int what_item)
        {
            CString txt = GetItemText(what_item);
            DWORD_PTR idata = GetItemData(what_item);

            if(true) //up to you to check if this item can be removed
            {
                OnAfterRemoveItem(txt,idata);
                return TRUE;
            }
            return FALSE;
        }

        void OnAfterRemoveItem(const CString& txt, DWORD_PTR idata)
        {
            CString info;
            info.Format(L"Removing item:'%s'",txt);

            MessageBox(info);
        }
};

Hope it helps.

Albertino80
  • 1,063
  • 7
  • 21