View contents of this chapter

I/O Example

In this section, we will develop a small program that uses the MsgBox() and InputBox() functions. Start a new project and place one text box and two command buttons on the form. Your application should look like this.

Set the following properties

Control Property Value
Form

Name

frmIO

Form

Caption

Input/Output

Text Box Name txtShow
Text Box Text Empty
Command Button 1 Name cmdMessage
Command Button 1 Caption Message Box
Command Button 2 Name cmdInput
Command Button 2 Caption Input Box

The source code needed for this example is stored in the
"Source Code\Chapter9\InputOptput" directory available in the CD. To open the project double click the "IO.VBP" file.

Private Sub cmdInput_Click()
   'Declare a string variable
   Dim strAns As String

   'Produces an input box
   strAns = InputBox("What is you name", "Enter your name")

   'assign the value of strAns to text box
   txtShow.Text = strAns
End Sub

Private Sub cmdMessage_Click()
   'Produces a message box
   MsgBox "Hello from VB", vbOKOnly, "Message Box"
End Sub

Please explain the code to me

When a user clicks the cmdInput button, an Input Box is shown to the user. If the user enters his name in the input box, that name is shown in the text box. If the user clicks the cancel button, the text box remains blank.

When a user clicks the cmdMessage button, a Message Box is shown to the user, which says "Hello from VB".



© Universal Teacher Publications        INDEX Previous Screen Next Screen