I have a CheckedListBox1 that contains checked and unchecked Items.
What I am trying to do is to save everything into a vector.
Like below, I can save all Items Text into vector<string> TextInfo but what I am trying to
do is also for each Item save if that specific Item was checked or unchecked.

How is this possible to do, because this might not be a String^ info that I can save into a String^ I beleive ?
I can get the CheckState for each Item like the codeline below.
What I wonder is how it will be possible to save the checkedstate for each Item.
I might need a vector in any way for this ?

CheckedListBox1->GetItemCheckState(i); 

//i is the index in a for loop like the code below
String^ Temp = "";
std::string temp = "";
std::vector<string> TextInfo;

if (CheckedListBox1->Items->Count > 0)
{ 
      for(int i = 0; i < CheckedListBox1->Items->Count; i++)
     {
     Temp = CheckedListBox1->GetItemText(CheckedListBox1->Items[i]);
     MarshalString(Temp, temp);
     TextInfo.push_back(temp);
     }
}

Recommended Answers

All 7 Replies

Edward has two suggestions. First, you can use the std::pair template from the utility header and store both the item text and check state:

std::vector<std::pair<String^, CheckState^> > ItemInfo;

for (int i = 0; i < CheckedListBox1->Items->Count; ++i) {
  String^ text = CheckedListBox1->GetItemText(CheckedListBox1->Items[i]);
  CheckState^ state = CheckedListBox1->GetItemCheckState(i);

  ItemInfo.push_back(std::make_pair(text, state));
}

Second, if you need more than one property from the item, why not just store the whole item along with the index? This way you have everything you need to access all of the properties:

std::vector<std::pair<int, Object^> > ItemInfo;

for (int i = 0; i < CheckedListBox1->Items->Count; ++i)
  ItemInfo.push_back(std::make_pair(i, CheckedListBox1->Items[i]));

This really seems to be a good approach. I have tried to compile this but the compiler
says something about that you cant delcare managed within unmanaged.
I beleive this is ment for <int, Object^> since this is in std::vector etc... ?

I am not sure what could be done or changed in this code to make it work ?
Thanks..

std::vector<std::pair<int, Object^> > ItemInfo;

for (int i = 0; i < CheckedListBox1->Items->Count; ++i)
{
        ItemInfo.push_back(std::make_pair(i, CheckedListBox1->Items[i]));
}

I found info on msdn and understand that I should declare it like this.
I beleive this could be right since std::pair has 2 unmanaged types.

std::vector<std::pair<int, string> > ItemInfo;

Hmm, I could have sworn that managed types and templates played nicely. Well, you can fix it by skipping over the template stuff and going straight to a full blown managed type:

List<KeyValuePair<int, Object^>> ItemInfo;

for (int i = 0; i < CheckedListBox1->Items->Count; ++i)
{
  ItemInfo.Add(KeyValuePair<int, Object^>(i, CheckedListBox1->Items[i]));
}

No problem, that sounds like a good idea to go managed here.
I have tried to compile this line and the compiler says that List and KeyValuePair are undeclared identifiers and type 'int' unexpected.
I am not sure if I have to #include something for this...

List<KeyValuePair<int, Object^>> ItemInfo;

You are missing using namespace System::Collections::Generic;

Thank you, that worked great...

//

You are missing using namespace System::Collections::Generic;

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.