Messagebox detect YesNoCancel button
Solution 1
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.
Solution 2
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
Solution 3
Im not sure about how to do this in C++ Building, but you need to enable I think something like multybit characters, but you need check against the documentation with your compiler.
Related videos on Youtube
user1690531
Updated on July 09, 2022Comments
-
user1690531 almost 2 years
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.
-
user1690531 over 11 yearsThanks. What is the best way to detect if the yes, no or the cancel button is pressed? Do I place the Messagebox result into a variable and then use an if statement to detect the button, or some other method?
-
Zdeslav Vojkovic over 11 yearsIf you need to handle all 3 cases differently then yes, you should store it in a variable and use
if
orswitch-case
statement to react accordingly. Otherwise, your code is fine, but then the question is why not just have MB_YESNO. -
user1690531 over 11 yearsI am just thinking of other situations where I might like more than two message buttons. Do I return the variable from the messagebox into an int, or is there a specific type for messagebox returns?
-
Zdeslav Vojkovic over 11 yearspressing F1 will show the documentation :). As I said, I don't use C++ Builder, so I have also looked up all of this on Google.
-
Adam McKee over 11 yearsI've updated my answer. Try not to ask more than one question in each post.
-
user1690531 over 11 yearsOk, sorry. About the CString, I have added the following with no luck: #include <atlstr.h>. What do I need to do to get CString working?
-
Adam McKee over 11 yearsI'm not sure, have you tried adding <cstringt.h>? Here's the documentation - msdn.microsoft.com/en-us/library/5bzxfsea.aspx
-
user1690531 over 11 yearsI have tried both cstringt.h and atlstr.h, with no luck. I am using C++ Builder. Is there an equivalent piece of code that can work that you know of in C++ Builder.
-
Remy Lebeau over 11 years
CString
is a Microsoft MFC class. For C++Builder VCL, useWideString
(C++Builder 2007 or earlier) orUnicodeString
(C++Builder 2009 or later) instead, eg:WideString s = "You have " + IntToStr(integerNumberOfImportantAppointments) + " important appointments. Do you wish to view them?"; MessageBox(NULL, s.c_bstr(), ...);
orUnicodeString s = "You have " + IntToStr(integerNumberOfImportantAppointments) + " important appointments. Do you wish to view them?"; MessageBox(NULL, s.c_str(), ...);