如何使用 C++ 形式的 TextBox 文本?

How to use TextBox text in c++ form?

我想知道有没有人知道如何将 C++ windowsform 中的 textbox 文本分配给字符串?
在 C# 中,例如:

string name;
name=textbox1.Text;

但在 C++ 中我不知道它是如何工作的。 我试过这个:

string name;
name = name_2door_txt->Text;

但是视觉上给我这个错误:

    IntelliSense: no operator "=" matches these operands
        operand types are: std::string = System::String ^   

我需要它是一个字符串。你能帮忙吗?

在 system::string 的参考资料中,您可以找到一些转换函数。例如,有 toCharArray() 函数。这样你就可以做到:

std::string name(name_2door_txt->Text.toCharArray());

参考:https://msdn.microsoft.com/en-us/library/vstudio/system.string(v=vs.100).aspx

试试这个:您需要为 System::String 对象创建句柄 (^)

System::String^ name = name_2door_txt->Text;

请包含以下头文件

#include <msclr\marshal_cppstd.h>

然后尝试

msclr::interop::marshal_context context;
std::string std_string= context.marshal_as<std::string>(name_2door_txt->Text);

如果你想转换成托管字符串

System::String^ managed_string = name_2door_txt->Text;

改成这样:

    System::String^ name;
    name = textbox1->Text; // VisualC++

将名称传递给native/unmanaged c++

using namespace System::Runtime::InteropServices;

std::string nName = static_cast<const char*>( Marshal::StringToHGlobalAnsi(name).ToPointer() );
callNative( nName ); // Call to C++ native