list item deleted event?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex K.

    list item deleted event?

    Hi all

    How do I catch an event when list item is deleted from ListBox?
    Is there such event?

    Thank you
    Alex
  • Frans Bouma [C# MVP]

    #2
    Re: list item deleted event?

    Alex K. wrote:
    [color=blue]
    > Hi all
    >
    > How do I catch an event when list item is deleted from ListBox?
    > Is there such event?[/color]

    I can't see it, but what you should do is this:
    - add the items to a collection which implements IBindingList. You can
    do this by deriving a class from ArrayList. (if you're on .NET 2.0,
    skip this step and go for BindableList<T> ). For an example, see below

    - bind the collection to the listbox. An item deleted from the listbox
    is deleted from the underlying collection, so the underlying collection
    will fire ListChanged, and you can bind to that event.

    Example IBindingList on ArrayList:
    (it's a quick 'n' dirty' implementation just to make sure a bound
    control receives events from the bound collection)
    /// <summary>
    /// ArrayList which implements IBindingList
    /// </summary>
    public class BindableArrayLi st : ArrayList, IBindingList
    {
    #region Events
    public event System.Componen tModel.ListChan gedEventHandler ListChanged;
    #endregion

    #region Class Member Declarations
    private bool _surpressEvents ;
    #endregion

    /// <summary>
    /// Creates a new <see cref="BindableA rrayLlist"/> instance.
    /// </summary>
    public BindableArrayLi st()
    {
    _surpressEvents = false;
    }

    /// <summary>
    /// Creates a new <see cref="BindableA rrayLlist"/> instance.
    /// </summary>
    /// <param name="c">C.</param>
    public BindableArrayLi st(ICollection c) : base(c)
    {
    _surpressEvents = false;
    }


    /// <summary>
    /// Called when the list changed
    /// </summary>
    /// <param name="changeTyp e">type of change of the list</param>
    /// <param name="newIndex" ></param>
    public void OnListChanged(L istChangedType changeType, int newIndex)
    {
    if(_surpressEve nts)
    {
    return;
    }
    if(ListChanged! =null)
    {
    ListChanged(thi s, new ListChangedEven tArgs(changeTyp e, newIndex));
    }
    }


    /// <summary>
    /// Called when the list changed
    /// </summary>
    /// <param name="changeTyp e">type of change of the list</param>
    /// <param name="newIndex" ></param>
    /// <param name="oldIndex" ></param>
    public void OnListChanged(L istChangedType changeType, int newIndex,
    int oldIndex)
    {
    if(_surpressEve nts)
    {
    return;
    }
    if(ListChanged! =null)
    {
    ListChanged(thi s, new ListChangedEven tArgs(changeTyp e, newIndex,
    oldIndex));
    }
    }


    public void ReflectChangedS tate(ListChange dType changeType)
    {
    OnListChanged(c hangeType, 0);
    }


    /// <summary>
    /// Adds the specified value.
    /// </summary>
    /// <param name="value">Va lue.</param>
    /// <returns></returns>
    public override int Add(object value)
    {
    int index = base.Add (value);
    OnListChanged(L istChangedType. ItemAdded, index);
    return index;
    }


    /// <summary>
    /// Clears this instance.
    /// </summary>
    public override void Clear()
    {
    base.Clear ();
    OnListChanged(L istChangedType. Reset, 0);
    }

    /// <summary>
    /// Adds the range.
    /// </summary>
    /// <param name="c">C.</param>
    public override void AddRange(IColle ction c)
    {
    base.AddRange (c);
    OnListChanged(L istChangedType. Reset, 0);
    }

    public override void Remove(object obj)
    {
    int indexOf = base.IndexOf(ob j);
    if(indexOf<0)
    {
    return;
    }
    base.Remove (obj);
    OnListChanged(L istChangedType. ItemDeleted, indexOf);
    }

    public override void RemoveAt(int index)
    {
    base.RemoveAt (index);
    OnListChanged(L istChangedType. ItemDeleted, index);
    }

    public void AddIndex(Proper tyDescriptor property)
    {
    }

    public bool AllowNew
    {
    get
    {
    return false;
    }
    }

    public void ApplySort(Prope rtyDescriptor property,
    System.Componen tModel.ListSort Direction direction)
    {
    }

    public PropertyDescrip tor SortProperty
    {
    get
    {
    return null;
    }
    }

    public int Find(PropertyDe scriptor property, object key)
    {
    return 0;
    }

    public bool SupportsSorting
    {
    get
    {
    return false;
    }
    }

    public bool IsSorted
    {
    get
    {
    return false;
    }
    }

    public bool AllowRemove
    {
    get
    {
    return false;
    }
    }

    public bool SupportsSearchi ng
    {
    get
    {
    return false;
    }
    }

    public System.Componen tModel.ListSort Direction SortDirection
    {
    get
    {
    return new System.Componen tModel.ListSort Direction ();
    }
    }

    public bool SupportsChangeN otification
    {
    get
    {
    return true;
    }
    }

    public void RemoveSort()
    {
    }

    public object AddNew()
    {
    return null;
    }

    public bool AllowEdit
    {
    get
    {
    return true;
    }
    }

    public void RemoveIndex(Pro pertyDescriptor property)
    {
    }


    /// <summary>
    /// Gets / sets surpressEvents
    /// </summary>
    public bool SurpressEvents
    {
    get
    {
    return _surpressEvents ;
    }
    set
    {
    _surpressEvents = value;
    }
    }

    }


    FB


    --
    ------------------------------------------------------------------------
    Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
    My .NET blog: http://weblogs.asp.net/fbouma
    Microsoft MVP (C#)
    ------------------------------------------------------------------------

    Comment

    Working...