Listbox.Clear not clearing

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.
Graham HirstIT EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ramesh SrinivasTechnical ConsultantCommented:
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.

Graham HirstIT EngineerAuthor Commented:
Data is simply being added through Listbox.Items.Add(obejct) - there is no binding in use.
Graham HirstIT EngineerAuthor Commented:
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

Graham HirstIT EngineerAuthor Commented:
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.