如何使用c++形式的文本框

How to use TextBox text in c++ form?

本文关键字:文本 何使用 c++      更新时间:2023-10-16

我想知道是否有人知道我如何将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 ^   

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

请包含以下头文件

#include <msclrmarshal_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的参考中可以找到一些转换函数。例如,有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;

改成:

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

将名称传递给本机/非托管c++

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