VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



The joys of MsgBox's

by Dark Star X (2 Submissions)
Category: VB function enhancement
Compatability: Visual Basic 3.0
Difficulty: Beginner
Date Added: Wed 3rd February 2021
Rating: (10 Votes)

This article will show you how to fully extend the basic VB MsgBox() function. Learn how to add icons, multiple lines of text, change the title, etc. MsgBox's are used in just about every application, learn how to use them to their maximum potential!

Rate The joys of MsgBox's

Everyone uses MsgBox's, but most people just use it in the form of:


MsgBox("Hello World")


What most people don't realise is that the MsgBox function has a wide variety of functions.


The proper syntax of the function is:


MsgBox([Message], [options], [title], [Help File], [Context])


For the purpose of not getting into too much advanced stuff here, we won't go into [context], we'll just leave it as vbNull, or not put anything in there at all.


For [Message] You can put whatever you like surrounded in quotes. The basic string!

For [options] you can put a combination of items using the Or keyword. [options] allows you to specify the buttons on the MsgBox (ok, cancel, retry, help, etc.), and/or the icon that appears on it. There are a bunch of already defined constants that VB allows you to use (seperate by the Or keyword if you want to use more than one, which I have already mentioned).


vbCritical = The big red circle with an X in the middle of it

vbExclamation = The yellow triangle with the exclamation point in it

vbInformation = The white text bubble with the big i in it

vbQuestion = The white text bubble with the big question mark in it


Note: You cannot combine icons, if you try to, none of them will show up.


You can also set which buttons show up, the names of the constants pretty much resemble what they do, for example:


vbYesNo = Gives the user the option of hitting Yes or No

vbOKOnly = Gives the user only the option to hit OK

vbAbortRetryIgnore = Abort, Retry, and Ignore options are given.

vbMsgBoxHelpButton = This will make a help button, and can be combined with vbOKOnly and vbCancelOnly.


Try entering this into Visual Basic:

Call MsgBox("Cannot Find File", vbCritical Or vbAbortRetryIgnore)

Pretty neat eh?


The last two options we should look at are:


vbSystemModal, and vbApplicationModal

vbApplicationModal prevents the user from preforming any work in the app until a button it pushed. vbSystemModal keeps the MsgBox on top of all windows until a button is pushed.


The next setting is [title], this is pretty straight forward, as it sets the title for the MsgBox. If it is not set, the title will be the title of the application, NOT the form.


[help file] is the path to the help file (in quotations), that will pop up when the user clicks the Help button if you include one. A help button will only be there if yoiu include a vbMsgBoxHelpButton in your options.


Another great function of MsgBox's, are their ability to return values, for example, asking for confirmation, you could use some bit of code like:


Dim X

X = MsgBox("Are you sure?", vbInformation Or vbYesNo, "Confirmation")

If X = vbYes Then Call MsgBox("Thanks", vbOKOnly)

Visual Basic also has a list of constants used to evaluate return values resulting from MsgBox's. They are pretty self explanitory, I'm sure you wouldn't even need to browse through a list to get most, if not all of them, some of them are as follows:


vbYes = The 'Yes' button was pressed

vbNo = The 'No' button was pressed

vbCancel = The 'Cancel' button was pressed

vbAbort = The 'About' button was pressed

vbRetry = The 'Retry' button was pressed


I'm sure you get the idea :)


Last thing we're gonna look at, is making multiple lines of text. It's actually very very easy, by using the vbCrLf constant that VB has. When making your [Message] try throwing it in as you would a TextBox. For example:


Call MsgBox("Hello" & vbCrLf & "World")



Well, there's my input for today, you should all be experts on MsgBox's now, I hope I helped at least one person, I know it's a bit useless, but everyone uses MsgBox's, and it's good to know what you can do, just in case you get stuck.


Please vote for me :)


Regards,

DarkStarX

Download this snippet    Add to My Saved Code

The joys of MsgBox's Comments

No comments have been posted about The joys of MsgBox's. Why not be the first to post a comment about The joys of MsgBox's.

Post your comment

Subject:
Message:
0/1000 characters