0
votes
private: System::Void btn_entrar_Click(System::Object^  sender, System::EventArgs^  e) {

string btn_texto = txt_login->Text->ToString();
MessageBox(NULL, "Hello!", btn_texto.c_str(), MB_OK | MB_ICONEXCLAMATION);

}

I'm creating a windows forms application normally in Visual C++ Studio 2008 Professional, I added a click function (by double clicking in the button on the design mode) so I wrote the code inside the function it generated.

it generated 2 errors:

Error 1:

error C2440: 'initializing' : cannot convert from 'System::String ^' to 'std::basic_string<_Elem,_Traits,_Ax>'

Error 2:

error C2872: 'MessageBox' : ambiguous symbol 1> could be 'C:\Program Files\Microsoft SDKs\Windows\v6.0A\include\winuser.h(7374) : int MessageBox(HWND,LPCTSTR,LPCTSTR,UINT)' 1> or
'c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll : System::Windows::Forms::MessageBox'

I'm creating in C++ on Visual Studio 2008 Professional at Windows 7 and the project under .NET Framework 3.5

Someone know how do I fix this? I looked it up all over but couldn't find a solution. I hope I gave enough information. Thanks in advance.

@edit Example Given by Cody Gray

System::String btn_texto = txt_login->Text->ToString();
System::Windows::Forms::MessageBox(NULL, "Hello!", "HI", MB_OK | MB_ICONEXCLAMATION);

Errors Gotten

error C3149: 'System::String' : cannot use this type here without a top-level '^' error C2661: 'System::Windows::Forms::MessageBox::MessageBox' : no overloaded function takes 4 arguments

So I solved the first error by adding ^ after System::String being like this:

System::String^ btn_texto = txt_login->Text->ToString();

but the second error wasn't fixed, and by the way, how would I add the "btn_texto" content in the MessageBox Function? Thanks!

2

2 Answers

2
votes
System::String^ btn_texto = txt_login->Text->ToString();
MessageBox::Show( "Working", "Info",    MessageBoxButtons::OK, MessageBoxIcon::Exclamation );

Solved it. :)

0
votes

You're mixing C++ string types (std::string) and .NET Framework string types (System::String) all over the place, and that's just not going to work out well. Or at least it's not going to be very simple. Decide which one you want to use and stick with it.

More specifically, the first error is caused by this line being wrong:

string btn_texto = txt_login->Text->ToString();
  1. The first reason it's wrong is because it creates an object of type std::string (presumably, you have a using namespace std; statement at the top of your code file), which is the C++ string type, not the .NET Framework string type. The .NET Framework string type is the one that you'll probably want to use in a .NET WinForms application. You'll have to fully qualify the namespace as System::String.

  2. The second reason it's wrong is because it's pointless to convert a String object (as returned by the Text property) to a String object using the ToString() method. Leave that last function call off completely.

The second error is caused by the fact that the Win32 headers provide a MessageBox function, as does the .NET Framework. The compiler needs to know which one you want to call.

  1. To call the Win32 MessageBox function (like you're attempting to do now), you need to use the global scope resolution operator: ::MessageBox.

    But then you're going to run into the problem of not being able to convert from System::String into a C-style string. See this article for instructions on how to convert between the various string types available in C++/CLI.

  2. The simpler approach is probably to call the .NET version of the MessageBox function, which you can accomplish by fully qualifying like so: System::Windows::Forms::MessageBox. The advantage of this version is that you won't need to do any string conversion because it accepts a parameter of type System::String. The .NET MessageBox wrapper supports all the same options as the Win32 MessageBox function, but some of them are in a different order, so you'll need to pay careful attention to the documentation.