Dumb question... TextBox.Text in a MessageBox...

  • Thread starter Thread starter Sin
  • Start date Start date
S

Sin

I'm currently evaluating VC.NET as the new platform for the company I work
for and things are looking grim... We're up against another IDE which took
me about 5 minutes to master and I've been bitching at .NET for the whole
day now not being able to do something as simple as showing a textbox's
content in a damn MessageBox...

The TextBox.Text is a String... MessageBox takes a LPCSTR (plain single byte
char*)... All our current code base is NOT unicode and there is no way in
hell we're converting to unicode. We can live with the fact the interface
will be unicode, as long as the conversions aren't too much of a problem
since we're only using the GUI options for debugging purposes... Our "real"
interfaces will be VB.NET (with occasional C/C++ using plain Win32).

Anyways... I simply want to use the plain ascii version of MessageBox to
show the contents of a TextBox I've put on a form... Should be simple...
I've tried everything from ASCIIEncoding, casting, PtrStringsomething,
StringToHGlobal, etc and nothing works. While we're at it, I'll most likely
need to do the opposite (putting a plain char string into a String)...

Any help would be greatly appreciated....

Thanks alot!!!

Alex (chewing his keyboard!).
 
Hi,

I'm not clear why do you need to output .Text in
win32 MessageBox? If you're already using Winforms
why not use MessageBox::Show(textBox1.Text,"");

Converting from String:
IntPtr ptr = Marshal::StringToCoTaskMemAnsi(textBox1->Text);
::MessageBox(NULL, (char*)ptr.ToPointer(), "", MB_OK);
Marshal::FreeCoTaskMem(ptr);

Converting to String:

char* pStr = "test";
String* str = pStr;
//String* str = new String(pStr);
//String* str = Marshal::PtrToStringAnsi(pStr);
MessageBox::Show(str,"");

Hope that helps
 
I'm not clear why do you need to output .Text in
win32 MessageBox? If you're already using Winforms

The real reason was just to try and convert the ->Text member to a plain
ascii single byte char array... We have a huge MSVC6 codebase which is all,
without exception, using plain ascii chars. Most of this code is
crosscompiled into QNX, which is a real-time OS that is somewhat of a unix
clone. Most of these applications do not have a user interface and when they
do they are remote VB screens using OPC to communicate with the control
apps. We're currently looking at .NET for our new Windows dev environement
and even though most of our interfaces are deported, we sometimes have a
need to make a local debug interface. I'm currently evaluating .NET as this
GUI platform. It needs to be simple, cause we're up against National
Instruments CVI which is simple (but on the other hand it's plain C, which
is it's biggest drawback).

In other words, we do not wish to dive in the .NET framework. We want to use
the .NET windows forms as an addtional tool which will be somewhat detached
from our existing (and new) code. MFC is out of the question and win32 is
just too much trouble for the kind of stuff we need to do. I'm a win32
programmer myself, but many here are used to a VB style environment where
you drop a button, double click on it, then write the code. I'm guessing
..NET can be used for this, and I'm currently verifying it.

why not use MessageBox::Show(textBox1.Text,"");

Well I tried but I get errors which I coudln't fix :

MainForm.h(98): error C2039: 'MessageBoxA' : is not a member of
'System::Windows::Forms'
MainForm.h(98): error C2660: 'System::Windows::Forms::Control::Show' :
function does not take 2 arguments

My code :

....
using namespace System::Windows::Forms;
....
private: System::Void button1_Click(System::Object * sender,
System::EventArgs * e)
{
MessageBox::Show(textBox1->Text,"");
}
....

It looks to me like it's expanding MessageBox:: to MessagBoxA:: because of
the windows.h define or something... I do have windows.h included but it's
in (another) cpp file (and nowhere in any .h file)

I'm confused...

Converting from String:
IntPtr ptr = Marshal::StringToCoTaskMemAnsi(textBox1->Text);
::MessageBox(NULL, (char*)ptr.ToPointer(), "", MB_OK);
Marshal::FreeCoTaskMem(ptr);

Converting to String:

char* pStr = "test";
String* str = pStr;
//String* str = new String(pStr);
//String* str = Marshal::PtrToStringAnsi(pStr);
MessageBox::Show(str,"");

Hope that helps

Thanks alot, it's exactly what I was looking for!

Alex.
 
Yes, compiler is expanding the MessageBox macro and it
conflicts with MessageBox in Winforms.

Simple way to work around it is:

#undef MessageBox
MessageBox::Show(textBox1->Text,"");

If you want to save/restore the MessageBox macro, you can use
push_macro/pop_macro.

After MessageBox macro is #undef'ed you can refer to the actual
MessageBoxA/MessageBoxW by name if needed.
 
Back
Top