I would like to display the elements in a vector in a listbox. However, I'm constantly getting a error: error C2664: 'System::Windows::Forms::ListBox::ObjectCollection::Add' : cannot convert parameter 1 from 'std::basic_string<_Elem,_Traits,_Alloc>' to 'System::Object ^'
I am using windows form in c++/cli. this is the code:
for (size_t z = 0; z < container.size(); z++){
listBox_name->Items->Add(container[z]);
}
Based on the error message, your vector is a vector of
std::string
. Usemarshal_as
to convert thestd::string
to a managedString^
that the managed list box can accept.If you find you're doing this a lot, consider changing your vector of
std::string
to be a fully-managed type, such asList<String^>^
.