0 Replies - 876 Views - Last Post: 11 March 2014 - 01:48 PM

#1 andrewsw  Icon User is offline

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3473
  • View blog
  • Posts: 11,788
  • Joined: 12-December 12

Simpler MessageBox

Posted 11 March 2014 - 01:48 PM

Instead of this:
MessageBox.Show("Can we proceed?", "Meaningless Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question)

I can type this:
MyMsg("Can we proceed?", "YesNo", "Question")

I can still use the shorter versions:
MessageBox.Show("Have a great day!")    'now..
MyMsg("Have a great day!")

In particular, I don't like having to make up a caption (which no one notices anyway) just to get to the buttons argument.

Here is the code:
    Private Function MyMsg(ByVal message As String, Optional ByVal buttons As String = "OK", _
                                 Optional ByVal icon As String = "None") As Windows.Forms.DialogResult
        Dim msgButtons As Windows.Forms.MessageBoxButtons
        Dim msgIcon As Windows.Forms.MessageBoxIcon
        [Enum].TryParse(buttons, True, msgButtons)  'default on fail is OK
        [Enum].TryParse(icon, True, msgIcon)        'default on fail is None
        Dim msgResult As Windows.Forms.DialogResult
        msgResult = MessageBox.Show(message, Application.ProductName, msgButtons, msgIcon)
        Return msgResult
    End Function


This could be made Public and placed in a Module so that it is available throughout your Project/Solution.

You do need to know the textual equivalent for the buttons and icons ("YesNoCancel", "Error", etc.) but there are only a few of them. If you mis-spell one of these it will default to the usual OK and None values. Both arguments are optional and not case-sensitive.

For your convenience:

Buttons: AbortRetryIgnore, OK, OKCancel, RetryCancel, YesNo, YesNoCancel

Icons: Asterisk, Error, Exclamation, Hand, Information, None, Question, Stop, Warning
        [Enum].TryParse(buttons, True, msgButtons)  'default on fail is OK


This attempts to convert the word ("YesNo", etc.) to the equivalent buttons. If it fails, it reverts to OK. The 'True' value makes the parsing case-insensitive.
        msgResult = MessageBox.Show(message, Application.ProductName, msgButtons, msgIcon)


As I mentioned, no one reads the caption (the second argument) and it is a pain inserting this just to get to the buttons and icon arguments. I just want to use the application's name as the caption. There are about 12 different ways to obtain the application-name: using various methods to read assembly-information, even reading the first command-line argument. Application.ProductName should, or could, be the name of your application. It is determined in a quite involved way. However, if you have a doubt about what it will be, you can set it in your Project's Properties.

Of course, now that you have the code, you could use whatever you want as the caption.




I realise that enumerated values are much preferred over strings (MessageBoxButtons.YesNo rather than "YesNo"). I don't care in this instance, I'm tired of typing the full MessageBox syntax and it makes the code more difficult to read.

Note: If you need to supply the first argument (the Owner) then I would stick with the standard MessageBox rather than my version.

I also haven't included the option to set a default button. This could be incorporated, perhaps as an integer (optional) argument. But my intention was not to replicate the MessageBox entirely.

Added: Here is the version that has a fourth optional argument to specify the default button ("Button2"):
    Private Function MyMsg(ByVal message As String, Optional ByVal buttons As String = "OK", _
                    Optional ByVal icon As String = "None", _
                    Optional ByVal defaultButton As String = "Button1") As Windows.Forms.DialogResult
        Dim msgButtons As Windows.Forms.MessageBoxButtons
        Dim msgIcon As Windows.Forms.MessageBoxIcon
        Dim msgDefault As Windows.Forms.MessageBoxDefaultButton

        [Enum].TryParse(buttons, True, msgButtons)  'default on fail is OK
        [Enum].TryParse(icon, True, msgIcon)        'default on fail is None
        [Enum].TryParse(defaultButton, True, msgDefault)

        Dim msgResult As Windows.Forms.DialogResult
        msgResult = MessageBox.Show(message, Application.ProductName, msgButtons, msgIcon, msgDefault)
        Return msgResult
    End Function


This post has been edited by andrewsw: 13 March 2014 - 05:02 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1