Hello everyone,
I don't often use forums for getting answers, but I simply cannot seem to find how to fire a MessageBox under Borland Builder...I could use MessageDlg which works, but I only want an OK button, so I would rather go with MessageBox...code sample:

void __fastcall TForm1::cmdPlayClick(TObject *Sender)
{
        if (TForm1::txtLogin->Text=="")
        {
                ::MessageBox("Invalid Login Name!", "Error!", MB_OK, 0);
                return;
        }

With MessageDlg:

if (TForm1::txtPass->Text=="")
        {
                MessageDlg("Invalid Password!", mtError, mbOKCancel, 0);
                return;
        }
        TForm1::LoadLauncher();
}

NOTE: I know it defines mbOK as a variable for TMsgDlgButtons, but it fires an error that it's NOT a recognized variable...I may be overlooking something, but it's late and I've been coding for hours now just getting my ActiveX control to function without causing access violations at memory address 0x000000 (WideString function is your friend.) Next issue will be writing the code to open a file which is like an ini file, but has a different structure (ie: no "[]" but uses "=" as an assignment operator...I *think* I have the groundwork set, but am unfamiliar with C++ file manipulation, I had created a similar program in VB6 and the styles of coding are mixing in my head =)

Thanks in advance for any information.

Dani AI

Generated

You are bumping into three similar but different APIs in C++Builder, which is why the examples in help and the replies look inconsistent. In VCL/Win32 you typically use either the Win32 API ::MessageBox or the VCL wrapper Application->MessageBox, both of which take MB_* flags and return IDOK, IDCANCEL, etc. The CLX documentation quoted (with TMessageButtons() << smbOK) is from a different framework, which is why it did not line up with VCL code. Also note ’s follow‑up is right: compare the return value to IDOK (or similar), not to MB_OK.

Two clean, working options that avoid the CLX/VCL mixup:

// VCL wrapper (modal to your app)
int r = Application->MessageBox(L"Invalid login name!", L"Error",
                                MB_OK | MB_ICONERROR | MB_APPLMODAL);
if (r == IDOK) {
    // handle OK
}
// VCL MessageDlg with a single OK button
if (MessageDlg(L"Invalid login name!", mtError,
               TMsgDlgButtons() << mbOK, 0) == mrOk) {
    // handle OK
}

A few gotchas that often cause the "unrecognized mbOK" or signature errors seen above:

  • Include the right headers. For MessageDlg, add #include <Dialogs.hpp> (modern IDEs: #include <Vcl.Dialogs.hpp>). For Application->MessageBox or ::MessageBox, #include <vcl.h> is usually enough, or add #include <Windows.h>/<Windows.hpp>.
  • Do not mix CLX symbols (smbOK, TMessageButtons) with VCL ones (mbOK, TMsgDlgButtons).
  • In form methods, prefer an owner: ::MessageBox(Handle, L"...", L"...", MB_OK | MB_ICONWARNING);. 0 works, but the dialog may not stay on top of your form.
  • C++Builder 2009+ is Unicode by default. Use wide string literals (L"...") or the TEXT() macro to avoid implicit ANSI/wide conversions.

Recommended Answers

All 4 Replies

I don't have my Builder up anymore, but replace MB_OK with zero and see what happens.

*sigh*
I found my error, in Borland's help files, it gives an example of:

Application->MessageBox(const WideString Text, const WideString Caption, 
    TMessageButtons Buttons = TMessageButtons() << smbOK, 
    TMessageStyle Style smsInformation, TMessageButton Default = smbOK, 
    TMessageButton Escape = smbCancel);

My error was using it in my Unit1.cpp file. Since all my declarations and main code is in the header file, it gave mystery errors and I tried various implementations like TApplication::MessageBox(blah) and stopped getting errors with plain ::MessageBox(blah) I moved the code into my header file and it now works. snippet for future reference:

Application->MessageBox("Message", "Title", Buttons() << btnValue);
//btnValue is MB_OK, etc. for the various button styles.

Like I said previously, it was late and I had the too-many-hours-coding-brain-lock syndrome to recognize that issue. Figured I would reply with my fix. Thanks for your help...off to build my file handling routines =)

Hi,

Try this

if( MessageBox(0, (LPSTR) "testing", "Error",
MB_ICONWARNING | MB_OK ) ==MB_OK) //do whatever

regards

Bob

Hello everyone,
I don't often use forums for getting answers, but I simply cannot seem to find how to fire a MessageBox under Borland Builder...I could use MessageDlg which works, but I only want an OK button, so I would rather go with MessageBox...code sample:

void __fastcall TForm1::cmdPlayClick(TObject *Sender)
{
        if (TForm1::txtLogin->Text=="")
        {
                ::MessageBox("Invalid Login Name!", "Error!", MB_OK, 0);
                return;
        }

With MessageDlg:

if (TForm1::txtPass->Text=="")
        {
                MessageDlg("Invalid Password!", mtError, mbOKCancel, 0);
                return;
        }
        TForm1::LoadLauncher();
}

NOTE: I know it defines mbOK as a variable for TMsgDlgButtons, but it fires an error that it's NOT a recognized variable...I may be overlooking something, but it's late and I've been coding for hours now just getting my ActiveX control to function without causing access violations at memory address 0x000000 (WideString function is your friend.) Next issue will be writing the code to open a file which is like an ini file, but has a different structure (ie: no "[]" but uses "=" as an assignment operator...I *think* I have the groundwork set, but am unfamiliar with C++ file manipulation, I had created a similar program in VB6 and the styles of coding are mixing in my head =)

Thanks in advance for any information.

Sorry I've just seen that I made a boob, the code
should have been ...

if(MessageBox(0, (LPSTR) "testing","Error",
MB_ICONWARNING | MB_OKCANCEL ) ==IDOK) //do whatever


regards

Bob G

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.