I am using a VCL Forms application in C++ Builder. can I please have some help in some code to display a messagebox with YesNoCancel buttons and then detect if the yes, the no or the cancel button is pressed.
Here is my code:
if(MessageBox(NULL, "Test message", "test title", MB_YESNOCANCEL) == IDYES)
{
}
I have included the following:
#include <windows.h>
I am getting the following errors:
E2034 Cannot convert 'char const[13]' to 'const wchar_t *'
E2342 Type mismatch in parameter 'lpText' (wanted 'const wchar_t *', got 'const char *')
Update
Here is my code:
const int result = MessageBox(NULL, L"You have " + integerNumberOfImportantAppointments + " important appointments. Do you wish to view them?", L"test title", MB_YESNOCANCEL);
The value: integerNumberOfImportantAppointments is an integer. How can I display this in a messagebox?
I am getting the following error: Invalid Pointer Addition.
Also, can I choose the icon for the messagebox? A question in this case.
Here you go. You need to use wide characters in the call to MessageBox
and you need to store the result in a variable, before working out what to do next.
const int result = MessageBox(NULL, L"Test message", L"test title", MB_YESNOCANCEL);
switch (result)
{
case IDYES:
// Do something
break;
case IDNO:
// Do something
break;
case IDCANCEL:
// Do something
break;
}
Update, following question edit:
// Format the message with your appointment count.
CString message;
message.Format(L"You have %d important appointments. Do you wish to view them?", integerNumberOfImportantAppointments);
// Show the message box with a question mark icon
const int result = MessageBox(NULL, message, L"test title", MB_YESNOCANCEL | MB_ICONQUESTION);
You should read the documentation for MessageBox.
I have no experience with C++ Builder, but it seems that you are using ANSI strings where UNICODE (actually wide character, but let's ignore details for the moment) strings are required. Try this:
if(MessageBox(NULL, L"Test message", L"test title", MB_YESNOCANCEL) == IDYES)
Even better, to ensure that your strings are conforming to your app settings, you can use:
if(MessageBox(NULL, _T("Test message"), _T("test title"), MB_YESNOCANCEL) == IDYES)
This will result in wide (wchar_t*) strings being used in UNICODE builds, and narrow (char*) strings in non-UNICODE builds (see '_TCHAR maps to' part in the Project Options)
For more details, see here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With