Listbox.Items.Clear() does not remove items?

2,005 views
Skip to first unread message

Benj Nunez

unread,
Oct 28, 2008, 4:05:56 AM10/28/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Normally, in other languages like Delphi, I used to clear the contents
of the Listbox
before I fill it up with data. My C# code looks like this:

private void btnCheck_Click(object sender, EventArgs e)
{
listBox.Items.Clear();

foreach (string s in displayList)
{
listBox.Items.Add(s);
}
}

I noticed that whenever you enter values to the Listbox, instead of
removing the previous entries,
the succeeding values only gets appended. For example, on the first
pass I enter "abc", the output
is "abc". On the second pass, I enter "123", the values will now be
"abc" and "123" one for each line!

Is there a workaround on this? How do I clear the contents of the
Listbox? By the way I use Arraylist to store data prior to displaying
it to the Listbox.







Cerebrus

unread,
Oct 28, 2008, 8:13:36 AM10/28/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
ListBox.Items.Clear DOES remove all the items present in the ListBox.
I suspect that it is your ArrayList that still has the old items and
so they get added again.

Paul

unread,
Oct 28, 2008, 8:31:38 AM10/28/08
to DotNetDe...@googlegroups.com
Have you considered just binding the listbox to your arraylist?
--
_______________________________

Take care,
Paul

It is not we non-interventionists who are isolationists. The real isolationists are those who impose sanctions and embargos on countries and peoples across the globe and who choose to use force overseas to promote democracy. A counterproductive approach that actually leads the U.S. to be more resented and more isolated in the world.

Dr. Ron Paul

www.RonPaul2008.com

Benj Nunez

unread,
Oct 28, 2008, 9:08:51 PM10/28/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You're right! Hehe. Silly me. I figured this out a while back. Thanks
again. :-)

What I did then was to make a call to the Arraylist's Clear() method:

displayList.Clear();


It's working now.

uma shunmuganathan

unread,
Oct 29, 2008, 12:07:18 AM10/29/08
to DotNetDe...@googlegroups.com
add item in the listbox during runtime then store that item in the listbox using ini file in delphi program

Cerebrus

unread,
Oct 29, 2008, 3:19:33 AM10/29/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
You're welcome. Glad you got it working ! :-)
> > > it to the Listbox.- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages