471,350 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

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
Jan 19 '06 #1
1 5688
Alex K. wrote:
Hi all

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


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 BindableArrayList : ArrayList, IBindingList
{
#region Events
public event System.ComponentModel.ListChangedEventHandler ListChanged;
#endregion

#region Class Member Declarations
private bool _surpressEvents;
#endregion

/// <summary>
/// Creates a new <see cref="BindableArrayLlist"/> instance.
/// </summary>
public BindableArrayList()
{
_surpressEvents = false;
}

/// <summary>
/// Creates a new <see cref="BindableArrayLlist"/> instance.
/// </summary>
/// <param name="c">C.</param>
public BindableArrayList(ICollection c) : base(c)
{
_surpressEvents = false;
}
/// <summary>
/// Called when the list changed
/// </summary>
/// <param name="changeType">type of change of the list</param>
/// <param name="newIndex"></param>
public void OnListChanged(ListChangedType changeType, int newIndex)
{
if(_surpressEvents)
{
return;
}
if(ListChanged!=null)
{
ListChanged(this, new ListChangedEventArgs(changeType, newIndex));
}
}
/// <summary>
/// Called when the list changed
/// </summary>
/// <param name="changeType">type of change of the list</param>
/// <param name="newIndex"></param>
/// <param name="oldIndex"></param>
public void OnListChanged(ListChangedType changeType, int newIndex,
int oldIndex)
{
if(_surpressEvents)
{
return;
}
if(ListChanged!=null)
{
ListChanged(this, new ListChangedEventArgs(changeType, newIndex,
oldIndex));
}
}
public void ReflectChangedState(ListChangedType changeType)
{
OnListChanged(changeType, 0);
}
/// <summary>
/// Adds the specified value.
/// </summary>
/// <param name="value">Value.</param>
/// <returns></returns>
public override int Add(object value)
{
int index = base.Add (value);
OnListChanged(ListChangedType.ItemAdded, index);
return index;
}
/// <summary>
/// Clears this instance.
/// </summary>
public override void Clear()
{
base.Clear ();
OnListChanged(ListChangedType.Reset, 0);
}

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

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

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

public void AddIndex(PropertyDescriptor property)
{
}

public bool AllowNew
{
get
{
return false;
}
}

public void ApplySort(PropertyDescriptor property,
System.ComponentModel.ListSortDirection direction)
{
}

public PropertyDescriptor SortProperty
{
get
{
return null;
}
}

public int Find(PropertyDescriptor 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 SupportsSearching
{
get
{
return false;
}
}

public System.ComponentModel.ListSortDirection SortDirection
{
get
{
return new System.ComponentModel.ListSortDirection ();
}
}

public bool SupportsChangeNotification
{
get
{
return true;
}
}

public void RemoveSort()
{
}

public object AddNew()
{
return null;
}

public bool AllowEdit
{
get
{
return true;
}
}

public void RemoveIndex(PropertyDescriptor 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#)
------------------------------------------------------------------------
Jan 20 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by John Smith | last post: by
reply views Thread by Maurice | last post: by
13 posts views Thread by na1paj | last post: by
10 posts views Thread by Abubakar | last post: by
8 posts views Thread by Jeff Bown | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.