Add event handler for ListView Items_added

In a C # Windows Form Application; Is there an event handler for a ListView control that fires when items are added to list items?

+5
source share
5 answers

I would see here or here . This is more or less the same answer, written only in a variety of styles. Short version, add the ItemAdded event to the ListViewItemCollection.

+4
source

You do not need to edit another source!

Good: change from ListView to myListView

- - ItemAdd-Function! ... WndProc-Function.

: 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)

# -

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;
        }
    }
}

ItemAddedEvent. , , .

gegards raiserle

(: ;))

+8

, . :

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; }
}
+4

Another alternative is to store the elements in an instance of the ObservableCollection class, set ListView.ItemsSource to this collection, and subscribe to the ObservableCollection.CollectionChanged event.

+1
source

The structure does not define an event, such as ItemAdded. However, be sure to visit this workaround: Observer Template and ListView Advanced Event Model . For example, the following events are defined here:

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

All Articles