Click here to Skip to main content
16,019,107 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to everybody.

This works fine
C#
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:
C#
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
Updated 19-Jul-12 3:28am
v3

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.
 
Share this answer
 
Comments
y.baris 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);
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:
SQL
while(listBox1.SelectedItems.Count > 0)
{
   listBox1.Items.Remove(listBox1.SelectedItem);
}
 
Share this answer
 
Comments
y.baris 19-Jul-12 8:25am    
ok how I forgot "s" letter..Ok thanks it solved my problem

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900