Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:55:01+00:00 2026-06-07T22:55:01+00:00

This works fine: private void btnDelete_Click(object sender, EventArgs e) { for (int i =

  • 0

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]?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:55:02+00:00Added an answer on June 7, 2026 at 10:55 pm

    The reason the second example does not work is because you are trying to pass multiple items in the form of a collection to be removed at once.

    To remove an item you need to do it one at a time, hence why you need a loop.

    Also, might I suggest using a ListView instead? I personally find them easier to use with many more options.

    For example you could make your loop for a ListView with just this

    foreach (ListViewItem item in listView1.SelectedItems)
    {
        item.Remove();
    }
    

    If you are trying to remove only one item at a time that is selected then you do

    listBox1.Items.Remove(listBox1.SelectedItem);
    

    SelectedItem rather than SelectedItems, plural being the collection that it can’t handle without a loop, singular being a single item that it can understand.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

private void CellFormatting(object sender, DataGridViewCellFormattingEventArgs e) { if(whatever) e.CellStyle.BackColor = SystemColors.ControlDark; This works fine
I am using silverlight 4, and the new async CTP. private void button1_Click(object sender,
This works fine: $.getJSON(http://localhost:59396/xxxWeb/ CarouselHandler.ashx?action=getproducts&ids= + ids, function(data) { carousel.size(allProductIDs.length); if (numberOfImagesLeftToShow < numberOfImagesToDisplay)
This works fine: GET /mvc/Movies/TitleIncludes/Lara%20Croft When I submit a request that contains a colon,
This works fine: [655971, 2343, 343].map(function(x) { return parseInt(x) }) // [655971, 2343, 343]
This works fine: TreeMap <String, ArrayList> x_probs_org = new TreeMap<String, ArrayList>(); But this: TreeMap
This works fine: var vm = {}; $.getJSON('file.json', function (data) { vm.objects = data;
I'm targeting the framework 4.0 and this works fine on the development machine, I
I´m dynamically creating an instance of a class with reflection and this works fine,
At the end of a ggplot, this works fine: + opts(title = expression(Chart chart_title...))

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.