How can I have a msgbox with two buttons, Validate and Cancel?
The message box contains two push buttons: OK and Cancel.
It displays a "Welcome" message box within a message box and an "OK" button. After clicking the OK button, another dialog box is displayed with a message "do you like the red color" and "yes", "no", and "cancel" buttons. After clicking any button (e.g., yes), the value of that button is stored as an integer.
Unfortunately, the standard Win32 MessageBox
function does not support buttons with custom labels. And since the VB.NET MsgBox
function is a thin wrapper over that native function, it doesn't support them either. The pre-defined values are all you get, meaning that the best you can do is something like "OK" and "Cancel", with text explaining that "OK" means to "proceed with validation".
It's worth noting that for years, this was the recommended practice endorsed by the Windows style guides. In fact, it looks like it still is. Specifically, note the exceptions to the following general rule:
Use positive commit buttons that are specific responses to the main instruction, instead of generic labels such as OK or Yes/No. Users should be able to understand the options by reading the button text alone.
Exceptions:
- Use Close for dialogs that don't have settings, such as informational dialogs. Never use Close for dialogs that have settings.
- Use OK when the "specific" responses are still generic, such as Save, Select, or Choose.
- Use OK when changing a specific setting or a collection of settings.
- For legacy dialog boxes without a main instruction, you can use generic labels such as OK. Often such dialog boxes aren't designed to perform a specific task, preventing more specific responses.
- Certain tasks require more thought and careful reading for users to make informed decisions. This is usually the case with confirmations. In such cases, you can purposely use generic commit button labels to force users to read the main instructions and prevent hasty decisions.
Windows Vista introduced a new API intended to replace the aging and impossible-to-configure MessageBox
—it's called the TaskDialog
. If you're running either Windows Vista or 7, you've undoubtedly seen this dialog used throughout the Windows shell. It does, in fact, allow you to specify custom names for each of the buttons, and provides an array of other customization options as well. But this kind of control doesn't come for free. There are two problems with the TaskDialog
function:
The .NET Framework does not include a wrapper for it out of the box. You'll need to either write your own simple wrapper, or download the Windows API Code Pack that includes such a wrapper. But this adds an extra dependency to your code—you'll have to decide whether that's worth it or not.
Because the API was only introduced in Vista, that means programs can only use it when running on Vista or later. If you still need to target Windows XP (or 2000), you're out of luck. The code to show a TaskDialog
will fail, and you'll need to include a fallback routine to show a standard MessageBox
. Which of course puts you right back where you started, without the ability to customize the button labels, on those legacy operating systems. Again, only you can decide if this is a significant concern for your application and/or deployment scenario.
The other option, used by generations of VB programmers, is to bang up your own little MessageBox
-style form. It's not that hard to lay out a simple form with a place for an icon/image, some caption text, and all the buttons you want. Since you've created the entire form, you'll be free to customize it from your code however you wish.
If you absolutely need this functionality in versions of Windows prior to Vista, then creating your own message box form is about your only option. Otherwise, I highly recommend that you take advantage of the new TaskDialog
API. Doing so within your application promotes consistency with other applications the user is likely to have installed on their computer, and even with Windows itself. It's difficult to ensure that your own custom message box form gets the little touches right, like automatically wrapping the label text depending on its length and the user's screen size. And showing/hiding the "X" close button in the title bar depending on whether your dialog includes a "Cancel" button. And an unbelievable number of other things that the standard Windows MessageBox
/TaskDialog
does for you for free without your having to lift a finger. This is just a restatement of the general principle: never re-invent the wheel when you don't absolutely have to do so.
A good compromise might be to use something like this TaskDialog
wrapper/emulator. On Vista and later, where the native TaskDialog
API is available, it automatically calls that function. Otherwise, it uses a standard form and attempts to simulate the behavior of the native TaskDialog
API. I've written a similar custom class for my own use, but I never got around to publishing it online.
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