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