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 6236401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:50:52+00:00 2026-05-24T10:50:52+00:00

When a box that is checked is unchecked I want to remove that item.

  • 0

When a box that is checked is unchecked I want to remove that item. The trouble is check/unchecking seems to happen after the ItemCheck method is called. So, when I remove an item that messes up the e.Index so it does the check/uncheck on the item after the one I remove or throws an error if it is the last one.

I found this: Getting the ListView ItemCheck to stop! which has the tip of resetting the e.NewValue which partially works, but it still throws an error when I remove the last item.

The reason I have not simply used one of the mouse events is that I want keyboard navigation to still be possible just in case.

Here is the code I have now.

private void checked_ItemCheck(object sender, ItemCheckEventArgs e)
        {
            if (e.NewValue == CheckState.Unchecked)
            {
                checked.Items.RemoveAt(e.Index);
                e.NewValue = CheckState.Checked;
            }
        }

Thanks for the help

  • 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-05-24T10:50:55+00:00Added an answer on May 24, 2026 at 10:50 am

    It sounds like the only problem you’re still experiencing is the call to e.NewValue after you remove the last item, right? If that’s the case, try this:

    private void checked_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        if (e.NewValue == CheckState.Unchecked)
        {
            checked.Items.RemoveAt(e.Index);
    
            // If there are no items left, skip the CheckState.Checked call
            if (checked.Items.Count > 0)
            {
                e.NewValue = CheckState.Checked;
            }             
        }
    } 
    

    UPDATE

    Ok – I got it to work, though I’m not sure how pretty it is. I used the SelectedIndexChanged event:

    private void checked_SelectedIndexChanged(object sender, EventArgs e)
    {
    
        CheckedListBox clb = (CheckedListBox)sender;
        int index = clb.SelectedIndex;
    
        // When you remove an item from the Items collection, it fires the SelectedIndexChanged
        // event again, with SelectedIndex = -1.  Hence the check for index != -1 first, 
        // to prevent an invalid selectedindex error
        if (index != -1 && clb.GetItemCheckState(index) == CheckState.Unchecked)
        {
            clb.Items.RemoveAt(index);
        }
    }
    

    I’ve tested this in VS 2010 and it works.

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

Sidebar

Related Questions

I want read-only check boxes to be greyed out, but display their checked/unchecked status
Another one cross-browser issue. JS logic: if one specific check-box is checked,that dependent ones
I have a checkbox when user checked that check box means. I need to
I have the following code that pushes check-box values to an array. I want
I have the following code. I want to make a check box that will
When Unlimited is checked, remove the input box. That works. However, when the checkbox
I have an input box that takes values from 0-100. I want to prevent
I want to have a text box that the user can type in that
I have an msi (authored with WIX) that has a check box bound to
I have got 2 check box[isSold(bit), Offline(bit)] in the datalist , I want to

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.