473,405 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 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 5791
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: John Smith | last post by:
Today I experienced something very very weird with STL. The code snippet below should work, but it doesn't with Microsoft VC++ 6.0. Ok what I have is a list of network connections. The code below...
0
by: Maurice | last post by:
how could you pass the head from a linked list class so that it could be used in a main file? here is the content of the linkedlist.h file: #ifndef LINKEDLIST_H #define LINKEDLIST_H ...
13
by: na1paj | last post by:
here's a simple linked list program. the DeleteNode function is producing an infinit loop i think, but i can't figure out where.. #include <stdio.h> typedef struct { char *str; //str is a...
10
by: Abubakar | last post by:
Hi all, I'm using stl's list to store some file names. Its declared as: list < char * filenames; i enumerate the list by using: list < char * >::const_iterator filename; filename =...
9
by: william | last post by:
When implementing Linked list, stack, or trees, we always use pointers to 'link' the nodes. And every node is always defined as: struct node { type data; //data this node contains ... node *...
2
by: jasone | last post by:
Hi, A section of my database allows users to delete items from a table, there is only one collumn in the table, this contains module information(parts of a manufacturing line) due to the lines...
8
by: Jeff Bown | last post by:
Consider implementing a (doubly) linked list. The simplest strategy is to provide functions item_t add_item(item_t predecessor); void delete_item(item_t item); where add_item allocates memory...
0
by: saijin | last post by:
Are there anyway that I can add a checkbox on a component that supports "dataProvider" and HTML tags? Here's what I have so far: import fl.data.DataProvider; import fl.managers.StyleManager;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.