• Log in or sign up for an account here: https://www.sythe.org/login

[TUT - VB6] Using Msgboxes and simple error handling

Blupig

BEEF TOILET
Joined
Nov 23, 2006
Sythe Gold
1,609
Messages
7,145
Age
33
Location
Canada
[TUT - VB6] Using Msgboxes and simple error handling
[TUT]Using an Msgbox to your Advantage & simple error handling


FYI

-FYI means For Your Information
-An msgbox is a Message Box to tell or display information


This is the syntax for a super simple msgbox (used with a command1):
Code:
Private Sub Command1_Click()
MsgBox "My tut is oobr."
End Sub

...But as you can see, there's no detail at all in this. It only shows an OK button, your project's name as a title and your message. You don't want this though right? You want nice looking and official msgboxes!

Before we make a nice looking official msgbox, I would like to show you what kind of things you can put into one.
Once you type msgbox in VB6, you will automatically get this in a yellow rectangle that pops up as a helper, but here it is anyways. This will also include the syntax for a complex msgbox:

Code:
MsgBox "Your message here", Button template + Image, "Title"
Possible button templates
-vbOKOnly (only the OK button, also default)
-vbAbortRetryIgnore (Abort, Retry and Ignore buttons are shown)
-vbYesNo (Yes and No buttons are shown)
-vbYesNoCancel (Yes, No and Cancel buttons are shown)
-vbOKCancel (OK and Cancel buttons are shown)
-vbMsgBoxHelpButton (OK and Help buttons are shown)
-vbRetryCancel (Retry and Cancel buttons are shown)

Possible Images
-vbCritical (red circle with a white "X" inside it)
-vbExclamation (yellow triangle with a black exclamation mark in it, a.k.a. 404)
-vbInformation (white message bubble with a blue "i" in it)
-vbQuestion (white message bubble with a blue question mark in it)

Possible 'other'
-vbMsgBoxRight (sets all writing excluding the command button to the right of the msgbox)
-vbMsgBoxRtlReading (completely inverts the msgbox from right to left, check this out it's trippy)
-vbSystemModal (a small image of the windows logo beside your title)

Using msgboxes with the "If" statement
You will need:
-2 radio buttons, default
-1 command button, default

Put the following code in your command1
Code:
Private Sub Command1_Click()
If Option1.Value = True Then
	MsgBox "You've selected " & Option1.Caption, vbInformation, "Option Selected"
End If

If Option2.Value = True Then
	MsgBox "You've selected " & Option2.Caption, vbInformation, "Option Selected"
End If
End Sub

All that code above in your command1 is really really simple. It's telling the program that if option1 is selected then you click command1, so show an msgbox telling you which option is selected, 1 or 2, thus the "& Option2.Caption". I chose to do this example with a caption, because users don't know the name of your controls, so the caption is like a user indentifier.

You can add an Else to this as well, and then ElseIf's, but let's just keep it at this for now. I wouldn't want to further confuse beginners.

Error Handling
This sounds real fancy, but it's really not. All you're are going to do is use the msgbox and the if statement to tell the user they made a mistake. Here's an example I use in my autotyper:
Code:
If txtInterval.Text > 60 Then
	MsgBox "Please choose an interval that is lower than 60.", vbExclamation, "Message Error"
	Exit Sub
End If

Now, this small If statement was used in my autotyper, where there's a timer interval being used. I made it so that the interval cannot go over 60, using only an If statement, an msgbox and an Exit Sub statement. This is telling my typer that if the text in the interval's textbox (txtInterval) is larger than (>) 60, to show the error message I created. Then, so that you don't get an application error message, you must put in the "Exit Sub" or else the program will close right after your msgbox.


This was a Tutorial gladly brought to you by Blupig.
If any more advanced programmers want to go more in depth with this, please add more on!
Also, if I made a mistake of any kind please to say so as well. I'm only an intermediate in VB6 myself, my greatest program being a human typer with mistakes, random interval, text effects and some other minor things.

Thanks, and use this knowledge for good not evil >:[
 
Runescape Gamblingrunescape gambling
Back
Top