7

Is there an option in Visual Studio to set all items in a CheckedListBox to checked by default? By this I mean I would like all items to be checked upon start up, and the user can unselect items as needed.

If not, is my only option to set all items to checked programatically inside the constructor?

AdamMc331
  • 16,492
  • 10
  • 71
  • 133

3 Answers3

11

You can do it programmatically after populating the Items

for (int i = 0; i < checkedListBox.Items.Count; i++)
{
    checkedListBox.SetItemChecked(i, true);
}
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
  • 2
    I think that's what I have to do. Not an issue - but surprised Visual Studio doesn't offer that property, or I am unbelievably blinded to it. – AdamMc331 Aug 29 '14 at 14:58
1
 private void Form1_Load(object sender, EventArgs e)
    {
       for (int i = 0; i < checkedListBox.Items.Count; i++)
       {
         checkedListBox.SetItemChecked(i, true);
       }  

    }
Rafik Bari
  • 4,867
  • 18
  • 73
  • 123
  • I'm using a CheckedListBox not a regular CheckBox. I can do this for a check box, but for some reason don't see this property for the checkedlistbox. – AdamMc331 Aug 29 '14 at 14:55
  • 1
    I don't understand how this is upvoted. Where is that property `isChecked` ? Can you upload a image? also I've never seen any property in .Net with camelCase! – Sriram Sakthivel Aug 29 '14 at 14:59
  • @SriramSakthivel the question was saying checkbox before the user edit the question. – Rafik Bari Aug 29 '14 at 15:08
  • 1
    I don't recall ever typing checkbox. – AdamMc331 Aug 29 '14 at 15:08
  • Question is never edited. Here is the [proof](http://stackoverflow.com/posts/25570739/revisions). I'd say you got upvotes for a crap answer. And still you're not ready to accept it. Blaming the OP! – Sriram Sakthivel Aug 29 '14 at 15:09
  • 1
    They did the same mistake as me. those who upvoted were just thinking the OP was talking about checkbox (: – Rafik Bari Aug 29 '14 at 15:12
  • 1
    In all fairness, it was a simple mistake and I do appreciate the help. If I were looking for a checkbox, that was a perfect answer. – AdamMc331 Aug 29 '14 at 15:22
0

try this i have used in VB.net

At the time of adding Items in CheckedListBox use below code :

For i As Integer = 0 To Dt.Rows.Count - 1
    CLB_FolderList.Items.Add(Dt.Rows(i)("Source_FolderName").ToString(),True)
Next
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • The question is tagged with C#, so please try to propose solution in that language. – Peter Csala Feb 11 '22 at 09:35
  • for (int i = 0; i < checkedListBox.Items.Count; i++) { checkedListBox..Items.Add(Dt.Rows(i)("Source_FolderName").ToString(), true); } Hi Peter, please check above code for C# when you are adding item in Control please above code. Thanks – Bhavik Shah Feb 15 '22 at 06:55