更改 C++ CLI 标签的文本
Changing the text of a C++ CLI label
我正在尝试更改 C++ CLI 程序中标签的文本。我需要获取用户在文本框中输入的值,将其插入一个短字符串,然后将标签更改为该字符串。我在构造字符串时没有问题,但在将标签设置为新字符串时遇到了问题。这是我的代码...
std::string v1str = "Phase A: ";
v1str.append(vt2); //vt2 is type str::string
v1str.append(" Vac");
label->Text = v1str;
这是我收到的错误消息...
为什么我不允许将 v1str
作为标签文本 setter 传递?如何将构造的字符串传递给标签文本 setter?
C++/CLI 不是 C++,你不能在那里使用 std::string
。但是您可以在 C++/CLI 中使用 C++,并将 std::string
与 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());
Label::Text
的类型为 System::String^
,这是一个托管的 .Net 字符串对象。您不能将 std:string
直接分配给 System::String^
,因为它们是不同的类型。
你可以convert一个std::string
到一个System::String
。但是,您很可能只想直接使用 System::String
类型:
System::String^ v1str = "Phase A: ";
v1st += vt2; // or maybe gcnew System::String(vt2.c_str());
v1str += " Vac";
label->Text = v1str;
我正在尝试更改 C++ CLI 程序中标签的文本。我需要获取用户在文本框中输入的值,将其插入一个短字符串,然后将标签更改为该字符串。我在构造字符串时没有问题,但在将标签设置为新字符串时遇到了问题。这是我的代码...
std::string v1str = "Phase A: ";
v1str.append(vt2); //vt2 is type str::string
v1str.append(" Vac");
label->Text = v1str;
这是我收到的错误消息...
为什么我不允许将 v1str
作为标签文本 setter 传递?如何将构造的字符串传递给标签文本 setter?
C++/CLI 不是 C++,你不能在那里使用 std::string
。但是您可以在 C++/CLI 中使用 C++,并将 std::string
与 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());
Label::Text
的类型为 System::String^
,这是一个托管的 .Net 字符串对象。您不能将 std:string
直接分配给 System::String^
,因为它们是不同的类型。
你可以convert一个std::string
到一个System::String
。但是,您很可能只想直接使用 System::String
类型:
System::String^ v1str = "Phase A: ";
v1st += vt2; // or maybe gcnew System::String(vt2.c_str());
v1str += " Vac";
label->Text = v1str;