Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 [Tutorial] Message Boxes
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
good_fella




PostPosted: Sun Nov 02, 2003 1:49 am   Post subject: [Tutorial] Message Boxes

There seems to be a lack of Visual Basic tutorials on this site. So here's one on message boxes. This is pretty basic stuff, but for people new to Visual Basic, it may be important to learn.

Message boxes are generally used to display short messages to users, such as errors, warnings, tips, etc. They are much more convienent then using seperate forms for these.

Here is the code for a message box that appears when you click a button called 'Command1'. Below is an explanation.

code:

Private Sub Command1_Click()

x = MsgBox ("Hello World.", vbExclamation + vbOKCancel + vbDefaultButton 1 + vbApplModal, "Hello World Message Box")

End Sub


In the above code, x can be any word suitable for the nature of the message box.

'MsgBox' is the command to load a message box.

"Hello World" is the text that appears inside the message box, which can be changed for any purposes.

'vbExclamation' loads the picture of an exclamation mark. This can be changed to other commands such as vbQuestion, vbCritical, or vbInformation. Don't worry about memorizing these as a list pops up that you can choose from when you start typing this part of the code.

'vbOKCancel' displays two buttons, the OK button and the Cancel button. This can also be changed to things such as vbYesNo, vbRetryCancel, vbYesNoCancel, etc. A list will appear allowing you to choose the apropriate command.

'vbDefaultButton1' will make the first button the default button. The 1 can be changed to a 2 or a 3 accordingly.

'vbApplModal' will wait for the user to respond to the message box before continuing the application. This can be changed to vbSystemModal in which all applications will be suspened until the user responds to the message box.

"Hello World Message Box" is simply the title of the message box that will be displayed in the title bar.

Finally, it is obviously important to know what button the user chooses inside the message box.

This is simply done by using an if statement.

Here is the code for determining which button is selected. This portion of the code would go below the code for the actual message box. It is explained below.

code:

If x = vbOK Then

Close
End

End If


Once again, x is simply the name you gave to the message box procedure.

'vbOK' is the command for the OK button being pressed. This can be changed to other values such as vbCancel, vbAbort, etc. A list will appear for you to choose from.

'Close' and 'End' will simply exit the program when the OK button is pressed. This was just my example, and can be changed to any number of things that you may want to occur after the user the picks an option from the message box.


Also note that in the actual code for the message box, you only need the 'x =' part if you plan on getting user input. Otherwise that can be left out. Also, anything after the actualy message appearing is optional. Any parts of it can be left out. An OK button will automatically be added to your message box if no other buttons are specified.

Also remember to seperate different commands for the message box by '+' signs not commas.

Well thats about all I can think of on creating a message box.
Once again if anything's unclear let me know.
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Sun Nov 02, 2003 4:43 pm   Post subject: (No subject)

a very good tutorial Very Happy +40Bits

The thing you can add is InputMsgBox (I think that's what it is). Which returns a string value typed in Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
GlobeTrotter




PostPosted: Wed May 18, 2005 3:21 pm   Post subject: (No subject)

You would have to declare "x" as an integer by:
code:

Dim x As Integer


This should be done right after private sub command1_click.
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Tutorials
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: