all groups > dotnet clr > june 2007 >
You're in the

dotnet clr

group:

Using MessageBox


Using MessageBox Allen Maki
6/3/2007 12:28:09 AM
dotnet clr:
/*

Console::WriteLine() I am trying to use message box instead of
Console::WriteLine() to upgrade my code from just using the console to using
GUI.

In the bottom 2 lines, #1 works, but I have hard time to make # 2 works. Can
anybody give a hand here to make the information to be shown on the message
box instead of being shown on the console.

I am using visual C++ .NET 2003

Thanks

*/



















#include "stdafx.h"

#include <gcroot.h>

#using <mscorlib.dll>

using namespace System;

using namespace System::Runtime::InteropServices;

typedef void* HWND;

[DllImportAttribute("User32.dll", CharSet=CharSet::Auto)]

extern "C" int MessageBox(HWND hw, String* text,

String* caption, unsigned int type);

__gc class MClass

{

public:

int val;

MClass(int n) //costructor

{

val = n;

}

};

class UClass

{

public :

gcroot<MClass*> mc;

UClass(MClass* pmc) //constructor

{

mc = pmc;

}

int getValue() //get function

{

return mc->val;

}

};

int _tmain()

{

Console::WriteLine(S"Testing...");

//Create a managed object and assign 3 to it

MClass* pm = new MClass (3);

//Create an unmanaged object and assign the value of the manged object (pm)
to it

UClass uc(pm);


Console::WriteLine(S"Value is {0}", __box(uc.getValue())); //<-------- This
will work # 1

//MessageBox(0, uc.getValue(), S"Message Box...", 0 ); //<------- this will
not work # 2





return 0;

}


Re: Using MessageBox Marc Gravell
6/3/2007 12:48:52 AM
MessageBox is not a method - it is a class with some static methods.

You also need to make sure that you reference System.Windows.Forms and
add System.Windows.Forms to your namespace list (I've never used
managed C++, but I'd guess (from your code) "using namespace
System::Windows::Forms;").

After that, you should be able to call MessageBox.Show(...)

If this doesn't work, please post the *actual* error message shown,
indicating whether it is compiler or runtime; copy and paste, not
paraphrased.

Marc
Re: Using MessageBox Allen Maki
6/4/2007 12:00:00 AM
/*

To Marc

Here is the error message.

Thanks.









Error message:



------ Build started: Project: messageBox, Configuration: Debug Win32 ------

Compiling...

stdafx.cpp

Compiling...

AssemblyInfo.cpp

messageBox.cpp

messageBox.cpp(13) : error C2039: 'Windows' : is not a member of 'System'

messageBox.cpp(15) : error C2144: syntax error : 'void' should be preceded
by ';'

messageBox.cpp(15) : error C2871: 'Forms' : a namespace with this name does
not exist

messageBox.cpp(15) : fatal error C1004: unexpected end of file found

Generating Code...

Build log was saved at "file://c:\project2\messageBox\Debug\BuildLog.htm"

messageBox - 4 error(s), 0 warning(s)



---------------------- Done ----------------------

Build: 0 succeeded, 1 failed, 0 skipped

*/





#include "stdafx.h"

#include <gcroot.h>

#using <mscorlib.dll>

using namespace System;

using namespace System::Runtime::InteropServices;

using namespace System::Windows::Forms

typedef void* HWND;

[DllImportAttribute("User32.dll", CharSet=CharSet::Auto)]

extern "C" int MessageBox(HWND hw, String* text,

String* caption, unsigned int type);

__gc class MClass

{

public:

int val;

MClass(int n) //costructor

{

val = n;

}

};

class UClass

{

public :

gcroot<MClass*> mc;

UClass(MClass* pmc) //constructor

{

mc = pmc;

}

int getValue() //get function

{

return mc->val;

}

};

int _tmain()

{

Console::WriteLine(S"Testing...");

//Create a managed object and assign 3 to it

MClass* pm = new MClass (3);

//Create an unmanaged object and assign the

//value of the manged object (pm)to it

UClass uc(pm);



Console::WriteLine(S"Value is {0}", __box(uc.getValue()));

//^-------- This will work # 1

//MessageBox(0, uc.getValue(), S"Message Box...", 0 );

//^------- this will not work # 2









return 0;

}



[quoted text, click to view]

Re: Using MessageBox Marc Gravell
6/4/2007 12:00:00 AM
You need to add a reference to System.Windows.Forms.dll.

I don't know managed C++, so I can't tell you how to do this (and I
can't be bothered to google for it). You also need a semi-colon after
Windows::Forms in the using statement.

Marc

Re: Using MessageBox Ben Voigt [C++ MVP]
6/4/2007 11:30:30 AM
[quoted text, click to view]

You should have asked this on the C++ group
(microsoft.public.dotnet.language.vc).

Couple points:
(1) Don't use C++.NET 2003 for anything. A lot of things were fixed in
2005, and the 2003 managed extensions won't be supported any longer.
(2) Don't use DllImport attribute in C++. Just #include <windows.h> to get
API functions.
(3) There is also a System::Windows::Forms::MessageBox::Show() function that
does message boxes the .NET way.

AddThis Social Bookmark Button