I have a CheckedListBox with 95 Checked Items and 5 Unchecked Items.
Every Checked and Unchecked Item has a "string" name.
What I am trying to do is to locate All the Unchecked Items in this box and put these strings into my vector with the pushback method: Unchecked.push_back();

The thing is that I am not sure how I will find all the Unchecked Items.
I have found ->CheckedItems but nothing that relate to Unchecked Items.
Perheps something with Checked == false could be used in any way here ?

std::vector<string> Unchecked;
CheckedListBox1->CheckedItems;

Recommended Answers

The Items property gives you everything, and from there it's no trouble to get the checked state for each item.

Jump to Post

The Items property represents a collection. You can use the Count property of the collection to query how many items are in the CheckedListBox.

Jump to Post

You have an off by one error. Indexes for the Items collection are zero based, so your loop should look like this:

for(int i = 0; i < CheckedListBox1->Items->Count; i++)
Jump to Post

All 9 Replies

The Items property gives you everything, and from there it's no trouble to get the checked state for each item.

Thanks. I have tried to come up with this code that checks for Items that are unchecked.
There is 1 thing that I might wonder.
In this case I know that there is 100 Items, so I use a for loop < 100.
Is there any way to loop to the END of the ListBox ?

std::vector<string> UncheckedLines;
std::string temp;
			 
for(int i = 0; i < 101; i++)
{
	String^ Temp = CheckedListBox1->SetItemCheckState(i, CheckState::Unchecked);

	MarshalString(Temp, temp);
				 				
	
	UncheckedLines.push_back(temp);
			
}

The Items property gives you everything, and from there it's no trouble to get the checked state for each item.

The Items property represents a collection. You can use the Count property of the collection to query how many items are in the CheckedListBox.

I have tried to put the code like this and also used the Count Property to know how many Items there is in the box.
When running this code I get an Error Message: Invalid Null Pointer

I beleive I am looping through the ListBox and check for Unchecked Items.
When I find a Unchecked Item I put this to the String^ Temp.
I convert Temp to a std::string temp and push_back this into the vector UncheckedSymbols.
Any idéas why I get this Errormessage ?

std::vector<string> UncheckedSymbols;
std::string temp;
String^ Temp;
			 
for(int i = 0; i < (CheckedListBox1->Items->Count + 1); i++)
{
	if( CheckedListBox1->GetItemCheckState(i) == ::CheckState::Unchecked )
	{
	Temp = CheckedListBox1->Items->ToString(); 
	MessageBox::Show(Temp); //Just Show the Unchecked index
	}

 MarshalString(Temp, temp);
					 
 UncheckedSymbols.push_back(temp);
}

You have an off by one error. Indexes for the Items collection are zero based, so your loop should look like this:

for(int i = 0; i < CheckedListBox1->Items->Count; i++)

Thanks, I did change to: for(int i = 0; i < CheckedListBox1->Items->Count; i++)
2 strange things is still happening though.
In the ListBox I do now have 8 checked Items. If I now execute the code I will still have this Error.
If I Uncheck the First Item in the listBox, I will will have a MessageBox appear.
The message in the MessageBox says:

System.Windows.Forms.CheckedListBox+ObjectCollection

If I uncheck Item 2 and 3, I will have 2 Messages like that wich anyway tell that the loop in a way is working.

What I am also after is the string that the Unchecked Item has. The Items has names like below.
So I am trying to get these stringnames to appear in the MessageBox when showing:
A
AB
ABC
ABCD

etc....


You have an off by one error. Indexes for the Items collection are zero based, so your loop should look like this:

for(int i = 0; i < CheckedListBox1->Items->Count; i++)

> If I now execute the code I will still have this Error.
Sorry, Edward was too lazy to read your code closely. The error is caused because you're trying to marshal a string that doesn't exist. Pretend that no items are unchecked for a minute and the if statement can go away. That makes the error easier to see:

std::vector<string> UncheckedSymbols;
std::string temp;
String^ Temp;

for(int i = 0; i < CheckedListBox1->Items->Count; i++)
{
  MarshalString(Temp, temp);
  UncheckedSymbols.push_back(temp);
}

You don't initialize Temp to an object, so it remains null. Ed suggests moving those two lines into the if statement along with both of the temporaries:

std::vector<string> UncheckedSymbols;

for(int i = 0; i < CheckedListBox1->Items->Count; i++)
{
  if( CheckedListBox1->GetItemCheckState(i) == ::CheckState::Unchecked )
  {
    String^ Temp = CheckedListBox1->Items->ToString();
    std::string temp;

    MessageBox::Show(Temp); //Just Show the Unchecked index

    MarshalString(Temp, temp);
    UncheckedSymbols.push_back(temp);
  }
}

> System.Windows.Forms.CheckedListBox+ObjectCollection
That's expected. The ToString method usually won't give you the property you want, which Ed guesses is the text property of an individual list item. Try this instead:

std::vector<string> UncheckedSymbols;

for(int i = 0; i < CheckedListBox1->Items->Count; i++)
{
  if( CheckedListBox1->GetItemCheckState(i) == ::CheckState::Unchecked )
  {
    String^ Temp = CheckedListBox1->GetItemText(CheckedListBox1->Items[i]);
    std::string temp;

    MessageBox::Show(Temp); //Just Show the Unchecked index

    MarshalString(Temp, temp);
    UncheckedSymbols.push_back(temp);
  }
}

Thank you for help !.

This did work very good...

Be a part of the DaniWeb community

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