P
Peter E. Granger
I'm having a strange problem (or at least it seems strange to me) trying to
display a MessageBox in a VC++ .NET forms application.
If I put the call to MessageBox::Show in the form's .h file, it works just
fine.
If I put the call in the .cpp file, I get the following two errors:
error C2653: 'MessageBoxA': is not a class or namespace name
error C2660: 'System::Windows::Forms::Control::Show': function does not take
2 arguments
I have tested this out as follows:
Create a .NET Windows Forms application with the default form, Form1, and
add two buttons to it, button1 and button2. Double-click on each button to
add the default event handlers. In the class declaration in Form1.h, I added
this:
// Declaration and definition together in .h file
private: System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
MessageBox::Show(S"Clicked Button 1",S"Title");
}
// Declaration only in .h file, definition in the .cpp file
private: System::Void button2_Click(System::Object * sender,
System::EventArgs * e);
Then in the class definition in Form1.cpp, I added:
// Definition in .cpp file corresponding to declaration in .h file
System::Void Form1::button2_Click(System::Object * sender,
System::EventArgs * e)
{
MessageBox::Show(S"Clicked Button 2",S"Title");
}
When I'm typing the code, I get the expected auto-completion after the
MessageBox:: so it seems that the System::Windows::Forms namespace is known,
as is the specific MessageBox object. System.Windows.Forms is included as a
reference in the project. I have also tried explicitly including
#using <System.Windows.Forms.dll>
using namespace System::Windows::Forms;
in the .cpp file, but that has no effect.
I tried to figure out what is going on with the reference to MessageBoxA in
the first error message. Searching in the help for MessageBoxA turns up a
reference to "Macros and the Preprocessor" which says
#ifdef UNICODE
#define MessageBox MessageBoxW
#else
#define MessageBox MessageBoxA
#endif // !UNICODE
So apparently UNICODE is not defined in my .cpp file, but it is in my .h
file?
Going on this I experimented -- adding
#include <windows.h>
to the .h file (not something you'd normally do -- it's an experiment)
causes the same two errors to occur in the .h file as in the .cpp file. So
now it looks like this is what's happening: Inclusion of windows.h in the
..cpp file causes MessageBox to be expanded into MessageBoxA which is
unknown. Absence of windows.h from the .h file causes MessageBox to be
expanded into MessageBoxW, which works. However, attempting to call
MessageBoxW explicity from the .cpp file gets me the same two errors again,
except for a different class:
error C2653: 'MessageBoxW': is not a class or namespace name
error C2660: 'System::Windows::Forms::Control::Show': function does not take
2 arguments
In short -- there doesn't seem to be any way to display a MessageBox from
the .cpp code of a form. Which leaves the option of displaying it from the
..h file, which is just wrong in so many ways, but gets the job done.
Suggestions? Thanks in advance.
display a MessageBox in a VC++ .NET forms application.
If I put the call to MessageBox::Show in the form's .h file, it works just
fine.
If I put the call in the .cpp file, I get the following two errors:
error C2653: 'MessageBoxA': is not a class or namespace name
error C2660: 'System::Windows::Forms::Control::Show': function does not take
2 arguments
I have tested this out as follows:
Create a .NET Windows Forms application with the default form, Form1, and
add two buttons to it, button1 and button2. Double-click on each button to
add the default event handlers. In the class declaration in Form1.h, I added
this:
// Declaration and definition together in .h file
private: System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
MessageBox::Show(S"Clicked Button 1",S"Title");
}
// Declaration only in .h file, definition in the .cpp file
private: System::Void button2_Click(System::Object * sender,
System::EventArgs * e);
Then in the class definition in Form1.cpp, I added:
// Definition in .cpp file corresponding to declaration in .h file
System::Void Form1::button2_Click(System::Object * sender,
System::EventArgs * e)
{
MessageBox::Show(S"Clicked Button 2",S"Title");
}
When I'm typing the code, I get the expected auto-completion after the
MessageBox:: so it seems that the System::Windows::Forms namespace is known,
as is the specific MessageBox object. System.Windows.Forms is included as a
reference in the project. I have also tried explicitly including
#using <System.Windows.Forms.dll>
using namespace System::Windows::Forms;
in the .cpp file, but that has no effect.
I tried to figure out what is going on with the reference to MessageBoxA in
the first error message. Searching in the help for MessageBoxA turns up a
reference to "Macros and the Preprocessor" which says
#ifdef UNICODE
#define MessageBox MessageBoxW
#else
#define MessageBox MessageBoxA
#endif // !UNICODE
So apparently UNICODE is not defined in my .cpp file, but it is in my .h
file?
Going on this I experimented -- adding
#include <windows.h>
to the .h file (not something you'd normally do -- it's an experiment)
causes the same two errors to occur in the .h file as in the .cpp file. So
now it looks like this is what's happening: Inclusion of windows.h in the
..cpp file causes MessageBox to be expanded into MessageBoxA which is
unknown. Absence of windows.h from the .h file causes MessageBox to be
expanded into MessageBoxW, which works. However, attempting to call
MessageBoxW explicity from the .cpp file gets me the same two errors again,
except for a different class:
error C2653: 'MessageBoxW': is not a class or namespace name
error C2660: 'System::Windows::Forms::Control::Show': function does not take
2 arguments
In short -- there doesn't seem to be any way to display a MessageBox from
the .cpp code of a form. Which leaves the option of displaying it from the
..h file, which is just wrong in so many ways, but gets the job done.
Suggestions? Thanks in advance.