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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:35:14+00:00 2026-05-17T22:35:14+00:00

I want to use a CheckedListBox in an application where each item in the

  • 0

I want to use a CheckedListBox in an application where each item in the ListBox is the name of a folder on my hard drive and for the purpose of reading and writing text files to and from each of these folders I want to ensure that one and only one item (a folder) can be selected at any one time in the CheckedListBox

How can I achieve this via code in C#?

Thanks for reading 🙂

Edit \ Update – 22/10/2010
Thanks to all who took the time to reply – especially Adrift whose updated code as requested is working perfectly.

I do appreciate what some commentators said about my usage of a checkedlistbox in this manner, however I feel it suits my purposes perfectly in that I want there to be no doubt whatsoever as to where the text files will be read from and written to.

All the best.

  • 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-17T22:35:14+00:00Added an answer on May 17, 2026 at 10:35 pm

    I agree with the comments that radio buttons would be the usual UI element when only a single item is ‘checked’, but if you want to stick with a CheckedListBox for your UI, you can try something like this:

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        CheckedListBox.CheckedIndexCollection checkedIndices = checkedListBox1.CheckedIndices;
    
        if (checkedIndices.Count > 0 && checkedIndices[0] != e.Index)
        {
            checkedListBox1.SetItemChecked(checkedIndices[0], false);
        }
    }
    

    You also might want to set CheckOnClick to true for the CheckedListBox.

    Edit

    Updated the code per your comment to deselect an item if it is unchecked. The problem is that unchecking the previously checked item causes the event to fire again. I don’t know whether there is a standard way to handle this, but in the code below, I detach the handler before calling SetItemCheck, then reattach the handler. It seems like a clean way to handle this, and it works. If I find that there is a recommended way to handle this, I will update my answer.

    HTH

    private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
    {
        CheckedListBox.CheckedIndexCollection checkedIndices = checkedListBox1.CheckedIndices;
    
        if (checkedIndices.Count > 0)
        {
            if (checkedIndices[0] != e.Index)
            {
                // the checked item is not the one being clicked, so we need to uncheck it.  
                // this will cause the ItemCheck event to fire again, so we detach the handler, 
                // uncheck it, and reattach the handler
                checkedListBox1.ItemCheck -= checkedListBox1_ItemCheck;
                checkedListBox1.SetItemChecked(checkedIndices[0], false);
                checkedListBox1.ItemCheck += checkedListBox1_ItemCheck;
            }
            else
            {
                // the user is unchecking the currently checked item, so deselect it
                checkedListBox1.SetSelected(e.Index, false);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want use JQuery mobile for the front-end of my mobile application, but I
We have a powerbuilder application and we want use a scanner through this application
I want use MVVM design pattern in WPF and Silverlight Application. Where can i
i want to use title on text. is this possible to make title on
I want use page that has this form: <form method=post action=modules.php?name=search> <input onkeypress=FKeyPress(this); onkeydown=FKeyDown(this);
guys I want to input multi-line text into a DataGridTextColumn, I can use enter
I want use a stored procedure to update multiple tables in a db. Each
i have an application for which i want use some reporting framework to generate
I want use WPF on Windows version of my java application. I found it
I have a CheckedListBox where I want an event after an item is checked

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.