Changing the text of a C++ CLI label

14,063

Solution 1

Label::Text has a type of System::String^, which is a managed .Net string object. You cannot assign a std:string to a System::String^ directly becuase they are different types.

You can convert a std::string to a System::String. However you most likely just want to use the System::String type directly:

System::String^ v1str = "Phase A: ";
v1st += vt2; // or maybe gcnew System::String(vt2.c_str());
v1str += " Vac";
label->Text = v1str;

Solution 2

C++/CLI is not C++, you can't use std::string in there. But you can use C++ within C++/CLI, and convert std::string to and from System::String

//In C++/CLI form: 
#include <vcclr.h>

System::String^ clr_sting = "clr_sting";

//convert strings from CLI to C++
pin_ptr<const wchar_t> cpp_string = PtrToStringChars(clr_sting);

//convert strings from C++ to CLI
System::String^ str = gcnew System::String(cpp_string);

//or
std::string std_string = "std_string";
System::String^ str2 = gcnew System::String(std_string.c_str());
Share:
14,063
JohnnyW
Author by

JohnnyW

Updated on June 04, 2022

Comments

  • JohnnyW
    JohnnyW over 1 year

    I am trying to change the text of a label in a C++ CLI program. I need to take a value the the user entered in a textbox, insert that into a short string, then change a label to that string. I have no problem constructing the string, but I am having trouble setting the label to the new string. Here is my code...

    std::string v1str = "Phase A: ";
    v1str.append(vt2); //vt2 is type str::string
    v1str.append(" Vac");
    label->Text = v1str;
    

    This is the error message that I'm getting...

    enter image description here

    Why am I not allowed to pass v1str as the label text setter? How can I pass the string I've constructed to the label text setter?