This for loop below sets 5 checkboxes in a checkedListBox. What I wonder is that when running the code below, 5 checkboxes is created and the checkboxes are "unchecked".
What I am wondering is how it is possible to make these 5 created checkboxes "checked" and how it is possible to set a "string" after each checkedbox, for ex: Word. So if 'v' is the checkbox. The example would look like this:

v Word
v Word
v Word
v Word
v Word

for(int i = 0; i < 5; i++)

{
	 CheckedListBox1->Items->Add(CheckedListBox1->CheckOnClick);
}

Dani AI

Generated

Good short summary and a working solution by — the Clear + Add + SetItemCheckState approach works fine. Below are why the first attempts behaved oddly, and cleaner, safer ways to do the same thing (C++/CLI WinForms).

The things that went wrong in the first two posts are just type/confusion issues: CheckOnClick is a Boolean property that controls click behavior (not the text to add), and CheckedItems is a collection of already-checked items — passing those into Items->Add(...) will add the property/collection itself, not a label you meant to show. (learn.microsoft.com)

Simpler: use the CheckedListBox Items.Add overloads that let you specify the checked state at insertion. That avoids a second loop to check items. Example (C++/CLI):

checkedListBox1->BeginUpdate();
for (int i = 0; i < 5; ++i)
    checkedListBox1->Items->Add("Word", true);    // add and mark checked
checkedListBox1->EndUpdate();

The Add(object, bool) and Add(object, CheckState) overloads let an item be added already checked (or indeterminate) so a separate SetItemCheckState loop is unnecessary. Use BeginUpdate/EndUpdate when adding many items for smoother redraws. (learn.microsoft.com)

Alternatives and tips: AddRange can load a whole array of strings, then call SetItemChecked(index,true) or SetItemCheckState(index,CheckState::Checked) if states must be changed later; both methods exist and both raise the normal ItemCheck event (ItemCheck fires when state changes — handlers should use e.NewValue or be deferred with BeginInvoke if code must run after the UI has the final state). Use CheckedItems / CheckedIndices to read checked values. (learn.microsoft.com)

Summary: the final loop that posted is valid; for a bit cleaner code prefer the Add("Label", true) overload (or Add with a CheckState) to populate already-checked items in one pass. (learn.microsoft.com)

I found out that this code do check all boxes. What I might wonder is how to put a "string" after a checkedBox.

for(int i = 0; i < 5; i++)

{
    CheckedListBox1->Items->Add(CheckedListBox1->CheckedItems);
				
}
 
for(int i = 0; i < 5; i++)

{
    CheckedListBox1->SetItemCheckState(i, CheckState::Checked);
}

I found a solution like this:

checkedListBox1->Items->Clear();
for(int i = 0; i < 5; i++)

{
				
	checkedListBox1->Items->Add("Word");
				
				
}



for(int i = 0; i < 5; i++)

{
				 				
	checkedListBox1->SetItemCheckState(i, CheckState::Checked);
				
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.