Remove items from the list box in C# windows application
-
Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet
foreach (object o in Lstservices.SelectedItems)
{
MessageBox.Show(Lstservices.SelectedItem.ToString());
Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);}
fttyhtrhyfytrytrysetyetytesystryrty
-
Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet
foreach (object o in Lstservices.SelectedItems)
{
MessageBox.Show(Lstservices.SelectedItem.ToString());
Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);}
fttyhtrhyfytrytrysetyetytesystryrty
-
Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet
foreach (object o in Lstservices.SelectedItems)
{
MessageBox.Show(Lstservices.SelectedItem.ToString());
Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);}
fttyhtrhyfytrytrysetyetytesystryrty
yadlaprasad wrote:
Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);
I don't think this'll work. Use this instead :
Lstservices.Items.Remove(o);
-
Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet
foreach (object o in Lstservices.SelectedItems)
{
MessageBox.Show(Lstservices.SelectedItem.ToString());
Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);}
fttyhtrhyfytrytrysetyetytesystryrty
use bellow
while (Lstservices.SelectedItems.Count>0)
{
Lstservices.Items.Remove(Lstservices.SelectedItem);
}Abdul Rahaman Hamidy Database Developer Kabul, Afghanistan
-
Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet
foreach (object o in Lstservices.SelectedItems)
{
MessageBox.Show(Lstservices.SelectedItem.ToString());
Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);}
fttyhtrhyfytrytrysetyetytesystryrty
All that your code does is to remove an item from the SelectedItems collection of your ListBox (i.e. you are unselecting it), not from the ListBox itself. To remove/delete from the listbox replace
Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);
with
Lstservices.Items.Remove(Lstservices.SelectedItem);
but bear in mind that, as someone else has mentioned, C# will complain if you try this whilst iterating over the collection.
Henry Minute Do not read medical books! You could die of a misprint. - Mark Twain Girl: (staring) "Why do you need an icy cucumber?" “I want to report a fraud. The government is lying to us all.”
-
Hi, i am trying remove some items from the listbox in C# windows application. but it doesn't work for me. can you please help?? below is the code snippet
foreach (object o in Lstservices.SelectedItems)
{
MessageBox.Show(Lstservices.SelectedItem.ToString());
Lstservices.SelectedItems.Remove(Lstservices.SelectedItem);}
fttyhtrhyfytrytrysetyetytesystryrty
With
foreach
, you are not allowed to alter the collection; you could use awhile
loop instead.