6

In C# Windows Form Application; Is there an event handler for the ListView control that is fired when items are added to the listview items ?

1

5 Answers 5

13

You dont need to edit other source!

Ok: change from ListView to myListView

Long time ago - but i search for an solution without implements with own ItemAdd-Function! The best way to do it... use the WndProc-Function.

Message: LVM_INSERTITEM

http://msdn.microsoft.com/en-us/library/windows/desktop/bb761107%28v=vs.85%29.aspx

//COMMCTRL.H
#define LVM_FIRST               0x1000           // ListView messages
#define LVM_INSERTITEMA         (LVM_FIRST + 7)  
#define LVM_INSERTITEMW         (LVM_FIRST + 77) 
//edit itemremove (LVM_DELETEITEM)
#define LVM_DELETEITEM          (LVM_FIRST + 8)

C#-implementation

class myListView : ListView {

    protected override void WndProc(ref Message m){
        base.WndProc(ref m);

        switch (m.Msg){
            case 0x1007:    //ListViewItemAdd-A
                System.Diagnostics.Debug.WriteLine("Item added (A)");
                break;
            case 0x104D:    //ListViewItemAdd-W
                System.Diagnostics.Debug.WriteLine("Item added (W)");
                break;
            //edit for itemremove
            case 0x1008:
                System.Diagnostics.Debug.WriteLine("Item removed");
                break;
            case 0x1009:
                System.Diagnostics.Debug.WriteLine("Item removed (All)");
                break;
            default:
                break;
        }
    }
}

Now you can fire your own ItemAddedEvent. I hope that helps other people, who have the same issue.

gegards raiserle

(edit: please vote ;) )

3
  • 1
    Thanks for sharing this info, may I ask what is the message or case for item removed?
    – NetInfo
    Commented Feb 6, 2014 at 16:52
  • 2
    msdn.microsoft.com/en-us/library/windows/desktop/… look at my post :D
    – raiserle
    Commented Feb 11, 2014 at 17:20
  • 3
    A note for future explorers... There's a specific message for "All items removed", value 0x1009 ;)
    – Ian
    Commented Nov 30, 2016 at 13:46
5

I would see here or here. They are more or less the same answer, just written in very different styles. Short version, add ItemAdded event to ListViewItemCollection.

0
5

There is no event that do that. But you can always create your own list box:

public class MyListView : ListView
{
    public void AddItem(ListViewItem item)
    {
        Items.Add(item);
        if (ItemAdded != null)
            ItemAdded.Invoke(this, new ItemsAddedArgs(item));
    }

    public EventHandler<ItemsAddedArgs> ItemAdded;
}

public class ItemsAddedArgs : EventArgs
{
    public ItemsAddedArgs(ListViewItem item)
    {
        Item = item;
    }

    public object Item { get; set; }
}
1

Another alternative is to hold the items in an instance of the ObservableCollection class, set ListView.ItemsSource to that collection and subscribe to the ObservableCollection.CollectionChanged event.

1
  • 1
    Sadly, Forms ListView doesn't have an ItemsSource. That is a WPF improvement. Commented Feb 5, 2015 at 18:44
0

The framework does not define an event such as ItemAdded. However, make sure to visit this workaround: An Observer Pattern and an Extended ListView Event Model. For example, the following events are defined there:

public event ListViewItemDelegate ItemAdded; 
public event ListViewItemRangeDelegate ItemRangeAdded; 
public event ListViewRemoveDelegate ItemRemoved; 
public event ListViewRemoveAtDelegate ItemRemovedAt;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.