What is the event on a listbox when adding/removing items ?

Hi everyone,

In my program, I have two different way to add objects in my listbox. One being by doing mylistbox.Items.Add(Myitem). and the other one by dataSource.

As found the event for the onDataSourceChanged but it doesn't triger when adding or removing from the items collection. His there any events I could use

thanks.



Answer this question

What is the event on a listbox when adding/removing items ?

  • Mohammed Allam

     abuck wrote:

    Hi everyone,

    In my program, I have two different way to add objects in my listbox. One being by doing mylistbox.Items.Add(Myitem). and the other one by dataSource.

    As found the event for the onDataSourceChanged but it doesn't triger when adding or removing from the items collection. His there any events I could use

    thanks.

    You can as well just run a method after you do "mylistbox.Items.Add(Myitem)", isn't it
    In case you want to catch the event in another class, you can declare your own event.



  • Julien Bonnier

    Yes I could do that but it is pretty anoying to call a function everytime i'm adding elements, I want to call a validation function.

    Also, I can't add an even on class that I don't have source code isn't ! I would need to override the add/rem function in a superior class.

    I'm just wondering if they have any event I can check..


  • Barrtee.s

    abuck wrote:

    Yes I could do that but it is pretty anoying to call a function everytime i'm adding elements, I want to call a validation function.

    Also, I can't add an even on class that I don't have source code isn't ! I would need to override the add/rem function in a superior class.

    I'm just wondering if they have any event I can check..

    If the listbox is in another class you can raise the event like you'd call the method. Since there's no (standard) event for this behaviour (at least I can't find any, just like you) there are not too many (other) options to accomplish it.



  • Green Lantern713

    That's basically what I said as well, but it includes raising the event after adding the item, instead of the event being raised automatically (like the datasourcechanged event).

    It's sad there's no existing event for this.



  • hfghgdhfghfghgd

    Yes this is sad but from a user point of view, the user will not be able to raise the event. For him, it will be raise automaticly with the superior class like Norbert Eder said. Ok that was my first idea, but I tought that it should have a built-in event for that.

    Thanks !


  • MichaelS

    It is not a nice solution, but it works:

    public class ListBoxEx : ListBox
    {
    public event EventHandler OnAdded;

    public void AddItem(object item)
    {
    base.Items.Add(item);
    if (this.OnAdded != null)
    this.OnAdded(this, new EventArgs());
    }
    }



  • What is the event on a listbox when adding/removing items ?