Listbox.Clear not clearing

Posted on 2008-11-04
Last Modified: 2013-12-17
I have a form with a button and a listbox.

When the button is clicked, it calls Listbox.Clear, and then fires off a new thread.
That thread, via an event (processing code which is on the form class) inserts items in the list.

I then click the button again, stick a breakpoint on List.Clear(), after its called the Count property gets set to 0, however the "entries" field (found inside "items") is still populated with all the objects I added.  As a result the second time round I duplicate the entries in the listbox.

I've tried calling Listbox.Refresh/Update all over the place, also using Listbox.Invoke to add the item on the same thread (dispite requires invoke returning false).  I'm very confused.

Additional info:
I don't kill/abort the thread at any point, just let it run to the end of the method in its threadstart.  When the button is clicked again, a new instance of the thread is created, which then runs.

Thanks in advance.
Question by:FSIFM
    LVL 11

    Expert Comment

    How are you binding data to the listbox?

    If you are using a datasource you need to set the listbox's datasource property to Nothing or null and rebind.

    LVL 4

    Author Comment

    Data is simply being added through Listbox.Items.Add(obejct) - there is no binding in use.
    LVL 4

    Author Comment

    If you make the change without using events, the problem does not occurs.  The code attached isolates the issue.

    Create a new C# windows app project, create a new listbox, and paste my code in.

    If you swap the commented lines in method AddItem, it works as expected.

    Any thoughts?
    public delegate void del(object sender, EventArgs e);
    public event del evt;
    private void listBox1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
    	this.evt += new del(addEvent);
    	System.Threading.Thread _thread = new System.Threading.Thread(new System.Threading.ThreadStart(AddItem));
    private void AddItem()
    	//listBox1.Items.Add("Data Item");
    	evt(this,new EventArgs());
    private void addEvent(object sender, EventArgs e)
    	listBox1.Items.Add("Data Item");

    Open in new window

    LVL 4

    Accepted Solution

    My colleague pointed it out to me.  It's because each time I click the list box, i'm adding a new event handler to the existing handler(s) each time, so it runs more than once.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now