Geekpedia Tutorials Home

Building a C# Chat Client and Server

Building a C# Chat Client and ServerA step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.

in C# Programming Tutorials

Getting Hard Drive Information

Getting Hard Drive InformationA C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.

in C# Programming Tutorials

UPS Shipping Calculator

UPS Shipping CalculatorThis tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.

in PHP Programming Tutorials

Create Your Own Rich Text Editor

Create Your Own Rich Text EditorCreating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.

in JavaScript Programming Tutorials
Search
Tutorials
Programming Tutorials
IT Jobs
From CareerBuilder

Remove selected items from a ListBox

On Sunday, November 27th 2005 at 05:00 AM
By Andrew Pociu (View Profile)
****-   (Rated 4 with 59 votes)
Advertisement
More C# Resources
Suppose you have a ListBox named listBox1. If you want to remove the selected item from it, use the Items.Remove() method, and pass as an argument the SelectedItem property:

listBox1.Items.Remove(listBox1.SelectedItem);


However if the ListBox has the SelectionMode set to MultiExtended and you want to remove all the selected items, use the following code:

while(lstKeys.SelectedItems.Count > 0)
{
   lstKeys.Items.Remove(lstKeys.SelectedItem);
}

Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this Knowledge Base article
Comment Current Comments
by on Friday, December 30th 2005 at 04:44 PM

does not work!

by Saxena on Tuesday, February 28th 2006 at 01:10 PM

Sorry but ListBox does not have SelectedItems

by Alberto Sefardi on Monday, March 13th 2006 at 11:03 AM

doesn't work

by Andrei Pociu on Monday, March 13th 2006 at 12:30 PM

What version of .NET are you using? Is it .NET 1.1 or .NET 2.0?
Thanks.

by GIGGS on Friday, March 17th 2006 at 07:18 AM

tried the same with listView - doesnt work ;/

by Domagoj on Monday, March 27th 2006 at 02:58 PM

works fine with visual studio 2005 and .NET framework 2.0 :-)

by Ivan on Thursday, April 6th 2006 at 12:06 PM

Hi, I just tried the above code in VS2003 and .Net 1.1: it works perfectly OK. Thanks.

by DBeals on Wednesday, April 19th 2006 at 12:53 AM

Saved me a lot of hair pulling, thanks!

by Darren on Thursday, June 1st 2006 at 11:50 AM

Hi Guys,

This is the corrent code for Visual Studio 2005, .NET 2.0. Should also work in 2003, .NET 2003 since the ListView Control is 1.0 or 1.1 anyway.

while (list.SelectedItems.Count > 0)
{
list.Items.Remove(list.SelectedItems[0]);
}

by 1 on Monday, July 24th 2006 at 09:47 AM

by 2 on Thursday, September 7th 2006 at 10:21 PM

2

by Sloan on Wednesday, September 27th 2006 at 02:38 PM

You gotta count down,

for (int i = lbxSelectedLocations.Items.Count - 1; i >= 0; i--)
{
if (lbxSelectedLocations.Items[i].Selected)
{
lbxSelectedLocations.Items.Remove(lbxSelectedLocations.Items[i]);
}
}

by Paul on Thursday, October 12th 2006 at 03:34 AM

Works fine with

while (list.SelectedItems.Count > 0)
{
list.Items.Remove(list.SelectedItems[0]);
}

Cool

by Aleksandar on Tuesday, November 7th 2006 at 05:19 PM

Thank you very very much

by Harpe on Wednesday, January 17th 2007 at 05:13 AM

Flemse edition

by Noobie on Monday, April 9th 2007 at 12:40 AM

Wow THANKS! I've been searching for this stupid script for AGES!
Works PERFECTLY!

by psyKo on Wednesday, April 11th 2007 at 11:01 AM

Congratulations! This is the first site, that I saw, that has a functional code to remove an item from a listbox!

by Gringo on Monday, April 16th 2007 at 10:26 AM

This one works for Web ListBox Control

for (int i = 0; i < ListBox.Items.Count; i++)
{
if (ListBox.Items[i].Selected)
{
ListBox.Items.Remove(ListBox.Items[i]);
i--;
}
}

by Zach Garrett on Monday, April 30th 2007 at 06:30 PM

You need to be careful

while (list.SelectedItems.Count > 0)
{
list.Items.Remove(list.SelectedItems[0]);
}

and


while (list.SelectedItems.Count > 0)
{
list.Items.Remove(list.SelectedItem);
}

may end up removing a lot more items than you expected if they are considered "equal". Try adding a bunch of strings with the same text and remove one of them.

by Zach Garrett on Monday, April 30th 2007 at 06:35 PM

This maybe a bit safer.

while( list.SelectedIndices.Count > 0 )
{
list.Items.RemoveAt(list.SelectedIndices[0]);
}

by Dominic on Wednesday, July 11th 2007 at 04:20 PM

Works just fine... thanks alot saved alot of time.

by James on Tuesday, August 14th 2007 at 06:29 AM

WOW thank god I found this it took me hours and its such a simple code haha.

by Nick on Wednesday, August 15th 2007 at 01:38 PM

Gotta specify which ListBox you\'re referring to, the System.Windows.Forms ListBox is completely different (and far superior to) the System.Web.UI ListBox. Web ListBox doesn\'t have SelectedItems, but Gringo\'s solution above works (though I\'m a bit leery about in loop modifications of a list...)

by joop on Saturday, August 18th 2007 at 05:34 AM

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
listBox1.Items.Remove(listBox1.SelectedItem);
}
work good!

by Mike on Monday, September 10th 2007 at 12:24 PM

Believe me, you want to use the code posted by Zach G above..

I have tried all solutions and that works the best. If you use remove(selecteditem) it seems to work but will remove more than what you want if the system deems to objects in your listbox equal. Most likely it will either only remove the first instance or both.

use:


while( list.SelectedIndices.Count > 0 )
{
list.Items.RemoveAt(list.SelectedIndices[0]);
}

Notice the removeAt versus Remove. Also, this works best when trying to synchronize removing from multiple listboxes because you can actually get the index you are looking for.

Thanks Zach!

by enas on Sunday, September 23rd 2007 at 06:03 PM

well..... this code worked with me:
for (int i = 0; i < ListBox.Items.Count; i++)
{
if (ListBox.Items[i].Selected)
{
ListBox.Items.Remove(ListBox.Items[i]);
i--;
}
}


THANX GRINGO so much :)

by meli on Wednesday, October 24th 2007 at 09:13 AM

Excellent ! Just what I was looking for... Thanks

by frank on Tuesday, November 13th 2007 at 04:24 AM

It works perfect with VS2003 and 1.1. Thanks!

by on Saturday, November 17th 2007 at 06:12 AM

while(this.lvPlaylist.SelectedItems.Count>0)
{
this.lvPlaylist.Items.Remove(this.lvPlaylist.SelectedItems[0]);
}

by Edmond on Saturday, April 5th 2008 at 06:30 AM

Ok so fine - but what about removing around three thousand entries at a time (i have a collection of the entries i\'d like to remove that i could create an array for instead with the indexes if necessary)...you have an AddRange but no RemoveRange or RemoveCollection - this makes things very slow.

by Regin on Thursday, October 23rd 2008 at 09:12 AM

Thank you! Gringo's solution works great on Web Listboxes.

by Thund3rstruck on Thursday, October 23rd 2008 at 11:03 AM

Actually no, it doesn't work because you can't remove items in a collection while you're still enumerating it. That's an InvalidOperationException and even if there are versions of .NET that allow this bad practice, it's going to cause memory leaks.

by Harry on Sunday, November 2nd 2008 at 02:04 PM

good

by eca on Monday, November 3rd 2008 at 05:06 AM

I'm so happy! It finally works!

by parvez on Thursday, January 15th 2009 at 06:48 AM

thanks it works

by Alfredo De Regil on Wednesday, April 15th 2009 at 11:23 AM

If you have a System.Web.UI.WebControls.ListBox called lbxDemo then the following code will remove all the selected items:

while (lbxDemo.SelectedItem != null)
{
lbxDemo.Items.Remove(lbxDemo.SelectedItem);
}

by Manu on Thursday, April 16th 2009 at 11:55 AM

What if the listbox is data binding. How would you delete from it?

by laxman4u on Friday, April 24th 2009 at 02:01 AM

i got right result

by Naseer ahamed M on Tuesday, May 12th 2009 at 01:27 AM

hi this is naseer,

ur article for my question was nice.
i come across some really good sollutions of my question with this.
thanks for ur article.

by ivan putra on Friday, June 12th 2009 at 04:16 AM

nice.
Thanks a lot!!!

by kishore on Thursday, June 18th 2009 at 03:45 AM

while(this.lvPlaylist.SelectedItems.Count>0)
{
this.lvPlaylist.Items.Remove(this.lvPlaylist.SelectedItems[0]);
}


good piece of code . reduced my work

by Pat on Monday, July 6th 2009 at 10:16 AM

it's work
Thank you

by jatinder on Tuesday, August 18th 2009 at 07:55 AM

protected void BtnRemoveEmail_Click(object sender, EventArgs e)
{
if (lstbxEmail.SelectedIndex != -1)
{
{
int j=0;

string SK = "";

j=lstbxEmail.Items.Count;

for (int i=0; i<j ;i )
{
if ((lstbxEmail.Items[i].Selected))
{

string SS = lstbxEmail.Items[i].Text;

SK = SK "/" SS;



}
}

string[] split = SK.Split(new Char[] { '/' });

for (int k = 0; k < split.Length; k )
{
if (split[k] != "")
{
lstbxEmail.Items.Remove(split[k]);
}
}
}

by Daniel on Thursday, August 20th 2009 at 10:49 PM

protected void eliminar_items(System.Web.UI.WebControls.ListBox ListBox)
{
try
{
ListItemCollection items_tmp = (ListItemCollection)(ListBox.Items);

foreach (ListItem item in items_tmp)
{
if (item.Selected)
{
ListBox.Items.Remove(item);
}
}
}
catch
{
eliminar_items(ListBox);
}
}

by Mukesh on Friday, September 25th 2009 at 08:14 AM

Hi This worked great for me..
private void btnRst_Click(object sender, EventArgs e)
{
while (lstXMLFiles.SelectedIndices.Count > 0)
{
lstXMLFiles.Items.RemoveAt(lstXMLFiles.SelectedIndices[0]);
}
}

by shashi kanth kangayam on Thursday, December 24th 2009 at 10:58 AM

while (list.SelectedItems.Count > 0)
{
list.Items.Remove(list.SelectedItems[0]);
}

this code saved my time !!!!
thank you very much

by shashi kanth kangayam on Thursday, December 24th 2009 at 10:58 AM

while (list.SelectedItems.Count > 0)
{
list.Items.Remove(list.SelectedItems[0]);
}

this code saved my time !!!!
thank you very much

by Mukesh Giri on Friday, December 25th 2009 at 02:02 PM

NP, You most welcome Shashi...

by Gabriel on Monday, January 11th 2010 at 01:41 PM

for (int i = 0; i < ListBox.Items.Count; i )
{
if (ListBox.Items[i].Selected)
{
ListBox.Items.Remove(ListBox.Items[i]);
i--;
}
}

WORKS PERFECT!!
THANKS!!!

by Amin_UODA on Thursday, January 14th 2010 at 06:52 AM

This really nice..........
Though i am new this environment.....i mean C#

by Amin_UODA on Thursday, January 14th 2010 at 06:53 AM

This really nice..........
Though i am new this environment.....i mean C#

by Russ on Friday, January 29th 2010 at 11:58 PM

Thanks a ton, this was really useful

by gemgreg on Tuesday, February 9th 2010 at 03:08 AM

simple solution:

while (listbox1.SelectedIndex != -1)
{
listbox1.Items.RemoveAt(listbox1.SelectedIndex);
}

by jarar on Sunday, March 7th 2010 at 11:11 AM

hi
any idea how to keep the selected highligh
to go up and down on the second item when ever we delete a row?

by Markus on Friday, March 12th 2010 at 01:14 AM

Works great... make sure you refresh the listbox after if need be.

by andy on Saturday, March 20th 2010 at 02:37 PM

i tried it, am using visual studio 2008 professional and .net 3.5 sp1

the best working and simple is:

while( list.SelectedIndices.Count > 0 )
{
list.Items.RemoveAt(list.SelectedIndices[0]);
}

hope it helps some1 :)

by damith on Wednesday, April 21st 2010 at 11:41 AM

for (int i = 0; i < ListBox1.Items.Count; i )
{
if (ListBox1.Items[i].Selected)
{
ListBox1.Items.Remove(ListBox1.Items[i]);
i--;
}
}

selected item remove.but item index not remove.Therefore create duplicate of below item.it is a problem.Thank you!!!!!!!

by damith on Wednesday, April 21st 2010 at 12:07 PM

it work. Thank you !!!!!!!

for (int i = 0; i < ListBox1.Items.Count; i )
{
if (ListBox1.Items[i].Selected)
{
ListBox1.Items.Remove(ListBox1.Items[i]);
i--;
}
}

by Arif on Sunday, May 2nd 2010 at 01:31 AM

Nice code, Thanxs

by Arif on Sunday, May 2nd 2010 at 01:31 AM

Nice code, Thanxs

by Amin on Sunday, May 2nd 2010 at 11:47 PM

I want very effective code.....

by sridhar on Friday, July 9th 2010 at 06:33 AM

This is code solved my problem. good one

by sridhar on Friday, July 9th 2010 at 06:33 AM

This is code solved my problem. good one

by Hanh on Friday, August 13th 2010 at 02:20 PM

both while and for loop work
while loop:
while (listbox1.SelectedIndex != -1)
{
listbox1.Items.RemoveAt(listbox1.SelectedIndex);
}

for loop

for (int i = 0; i < ListBox1.Items.Count; i )
{
if (ListBox1.Items[i].Selected)
{
ListBox1.Items.Remove(ListBox1.Items[i]);
i--;
}
}



by ibrahim on Wednesday, October 6th 2010 at 03:34 AM

First Solution has successfully Worked :)
thanx :)))))))

by AJ on Wednesday, November 10th 2010 at 10:49 AM

Whats the code for visual basic 6.0??? there is no lisbox1.items.remove.. it onli says removeitem but non of the above work :S

by Sulc on Friday, November 26th 2010 at 05:39 PM

Thanks for code.
But, I`ve got next problem:
I need to remove selected item from listBox and SQL database too. When I use classic code (which is showen here) I just remove from listBox.
Example:
In data row with Name, LastName
Jack Nickolson
Mila Jovovic

I wont clear from database some actor?
I now syntax: DELETE FROM actors WHERE FirstName='Mila' AND LastName='Jovovic'
but I don`t now how to implement in code?

Thank any way!

by riddhi on Tuesday, March 1st 2011 at 02:02 AM

no one work

by riddhi on Tuesday, March 1st 2011 at 02:02 AM

no one work

by Loganathan on Tuesday, April 5th 2011 at 08:20 AM

it works for my silverlight application also... thank u

by sabeersas on Tuesday, April 5th 2011 at 02:28 PM

Listbox1.Items.Remove(Listbox1.SelectedItem);

This code Will worked for me...no looping needed

by Jameel Badr AL Awadhi on Friday, May 6th 2011 at 12:14 PM

I'm Yemen exactly (Aden)

I wasn't know u , but when i got in your site
I love your site.

I thank all of u from your good information about C# code .

I hope the best 4 u , thank u very much and
I hope to enter The Islam , it is best regelion
if u (wash your body and said La Ilah Ella ALLAH and Mohummed Rasool ALLAH ) Yo'll enter the Islam and Allah will forgive u all thing u did it at the past , and u will enter the pradise


Your student
best regards

by Jameel Badr AL Awadhi on Friday, May 6th 2011 at 12:14 PM

I'm Yemen exactly (Aden)

I wasn't know u , but when i got in your site
I love your site.

I thank all of u from your good information about C# code .

I hope the best 4 u , thank u very much and
I hope to enter The Islam , it is best regelion
if u (wash your body and said La Ilah Ella ALLAH and Mohummed Rasool ALLAH ) Yo'll enter the Islam and Allah will forgive u all thing u did it at the past , and u will enter the pradise


Your student
best regards

by anon on Tuesday, June 21st 2011 at 10:11 PM

i just spent 3 hrs trying to figure this out! thnx!!!!!!!!!!!!!!

by apply for a credit card with bad credit on Sunday, December 18th 2011 at 12:34 PM

i liked it too much . better if u provide a program and give comments on it .such as a bank account program. Why don't you post your own solution Pavlo..... it would have sure taken less time than writing all that.

by Pratheeksha on Tuesday, January 31st 2012 at 11:38 PM

Hi..

I liked the solution and site.. It's really good
thanks..helped me a lot

by Parth on Tuesday, March 6th 2012 at 12:32 AM

Thank You so much.
I just wanna know the difference beetwen this two:

listbox1.Items.Remove(listbox1.SelectedItem);

by Parth on Tuesday, March 6th 2012 at 12:32 AM

Thank You so much.
I just wanna know the difference beetwen this two:

listbox1.Items.Remove(listbox1.SelectedItem);

by edmund on Wednesday, March 14th 2012 at 03:47 AM

how about it will not accept duplicate items in list box? can u show me the code? tnx

by sss on Thursday, May 3rd 2012 at 01:26 AM

sdfsfds

by sss on Thursday, May 3rd 2012 at 01:26 AM

sdfsfds

by ugeHidalgo on Wednesday, October 9th 2013 at 03:53 PM


As simply as...

listBox.Items.Clear();


Comment Comment on this Knowledge Base article
Name: Email:
Message:
Knowledge Base Related Knowledge Base Articles
There are no related KB articles.

Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs C# Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Ads

From the creators of Geekpedia, a revolutionary new coupon website!

BargainEZ has coupons codes, printable coupons, bargains and it is the leading source of Passbook coupons for iPhone and iPod touch devices.

Coupons
Discover Geekpedia
Other Resources