Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# selection
Hello to everybody.
 
This works fine
private void btnDelete_Click(object sender, EventArgs e)
{
    for (int i = 0; i < listBox1.SelectedItems.Count; i++)
    {
        listBox1.Items.Remove(listBox1.SelectedItems[i].ToString());
        i--;
    }
}
 
But this doesn't work:
private void btnDelete_Click(object sender, EventArgs e)
{
    listBox1.Items.Remove(listBox1.SelectedItems);
}
 
Why is the second btnDelete_Click not working? I mean I select a line on my listBox1 with my mouse and then press the button. Doesn't the .Remove function recognize which line I selected? Even though I say .Remove(listBox1.SelectedItem), is it a must to have and selectedItem array? Isn't the word SelectedItems self-explanatory? And since I clicked the line on my listBox1 with my mouse, can't the program or the IDE understand which line is selected? Why do I still have to use SelectedItems[i]?
Posted 19-Jul-12 3:09am
y.baris676
Edited 19-Jul-12 4:28am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

The "Remove" function lets you only remove one object at a time, but the "SelectedItems" function returns a collection of the selected items (since you may have multiple selected items). You could also use this code:
while(listBox1.SelectedItems.Count > 0)
{
   listBox1.Items.Remove(listBox1.SelectedItem);
}
  Permalink  
Comments
y.baris at 19-Jul-12 8:25am
   
ok how I forgot "s" letter..Ok thanks it solved my problem
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Why is the second btnDelete_Click not working:
 
Because the Remove method expects the element to remove but the SelectedItems property returns a collection of items.
 
I mean I select a line on my listBox1 with my mouse and then press the button. Doesn't the .Remove function recognize which line I selected? Even though I say .Remove(listBox1.SelectedItem), is it a must to have and selectedItem array? Isn't the word SelectedItems self-explanatory?
 
What has the generic Remove function of a collection to do with a GUI state like selected items? If you want a "Remove selected" function your first solution will work fine (except you don't need the ToString call). What you mean by self-explainatory? for the compiler? You don't do magic here - it's programming.
 
And since I clicked the line on my listBox1 with my mouse, can't the program or the IDE understand which line is selected?
 
This questions gives me a "strong hint" you are a beginner... There is no IDE at runtime, the "program" understands which lines are selected (you can call the SelectedItems property to retrieve them, you did that, didn't you?)
 
Why do I still have to use SelectedItems[i]?
because the Remove method want's the object to remove (like the docu/intellisense says). And with the Index operator you get one element out of a collection - the one you want to remove.
 
Conclusion: I admit that a RemoveRange method would be a nice feature for a listbox. But your other questions come from a lack of understanding some basics and it seems you don't have much experience with the framework - the good thing about .NET: it is very uniform, if you get it for the Items-collection you understand it for all collections (some of them have a RemoveRange method)
If you still don't like it. Implement your own Listbox (derive) or create an extension method for ListBox.ObjectCollection. In the end it's always the same, and for me a good thing about programming: If you don't like the way it is, create your own piece of code for your needs - and for this problem you can clearly do that - but I think you will stick to your first solution in the end.
  Permalink  
Comments
y.baris at 19-Jul-12 9:17am
   
Thanks for your advices.Also your hint is right :)
but as you and @JF2015 said I just forgot to delete "s" letter (SelectedItems wrong, SelectedItem is true ) cause I want to delete just one item.And my code must be changed like that below :
listBox1.Items.Remove(listBox1.SelectedItem);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 BillWoodruff 330
1 Mathew Soji 309
2 Maciej Los 295
3 DamithSL 225
4 Afzaal Ahmad Zeeshan 217
0 OriginalGriff 6,369
1 Sergey Alexandrovich Kryukov 5,973
2 DamithSL 5,183
3 Manas Bhardwaj 4,673
4 Maciej Los 4,025


Advertise | Privacy | Mobile
Web04 | 2.8.1411019.1 | Last Updated 19 Jul 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100