VB6 Tutorial 14: Input and Output

Posted On 2018-04-01 by VB6Boy
Keywords:
Tags: VB6 Tutorials Windows
Views: 46

Title Uploaded Size
InputBoxMsgBox01.zip 4/1/2018 11:23:25 AM 944
InputBoxMsgBox02.zip 4/1/2018 11:24:42 AM 1,058
PictureboxOutput.zip 4/1/2018 11:24:56 AM 1,139
SumCalculator.zip 4/1/2018 11:25:14 AM 1,225


Looking for the more of the VB6 Tutorial? Here is the tutorial index.

Visual Basic provides some excellent ways for input and output operations. Input can be taken using TextBox, InputBox, and output can be shown with the Print method, TextBox, Label, PictureBox and MsgBox.

Input and output using TextBox

The TextBox Control provides an efficient way for both input and operations. The following sample program shows it.

Example

Private Sub Command1_Click()
    Dim a As Integer
    a = Val(Text1.Text)
    a = a + 1
    Text2.Text = a
End Sub

In the above program, Input is taken using Text1 and output is shown in Text2, where output is the incremented value of the input.

See the attached program SumCalculator for a demonstration of input and output using textboxes.

The TextBox Control provides an efficient way for both input and operations. The attached project SumCalculator shows how.

InputBox

InputBox is a function that prompts for user-input. InputBox shows a dialog box that inputs value from the user.

Syntax

a=InputBox( promt, [Title], [Default], [xpos], [ypos])

where 'a' is a variable to which the value will be assigned. The texts inside the InputBox are optional except the "prompt" text. "prompt" text is the prompt message. "title" is the title of the message box window. "Default" is the default value given by the programmer. 'xpos' and 'ypos' are the geometric positions with respect to x and y axis respectively.

Quickhint:

Parameters in brackets are always optional. Do not write the brackets in your program.

Example:

Private Sub cmdTakeInput_Click()
    Dim n As Integer
    n = InputBox("Enter the value of n : ")
    Print n
End Sub

The above program prints the value of n taken from the InputBox function.

Example

InputBox with the title text and default value.

Private Sub cmdTakeInput_Click()
    Dim n As Integer
    n = InputBox("Enter the value of n : ", "Input", 5)
    Print n
End Sub

The InputBox dialog appears with the title "Input" and the highlighted default value in the provided text field is 5.

MsgBox

The MsgBox function shows a dialog box displaying the output value or a message.

Syntax

MsgBox Prompt, [MsgBox style], [Title]

where Promt text is the prompt message, [MsgBox Style] is the msgbox style constants and [Title] is the text displayed in the title bar of the MsgBox dialog.

Example

Private Sub cmdShowMessage_Click()
    Dim n As Integer
    n = 10
    MsgBox "The value of n is " & n
End Sub

Example

MsgBox with the dialog type and title text.

Private Sub cmdShowMessage_Click()
    Dim n As Integer
    n = 10
    MsgBox "The value of n is ", vbInformation, "Result" & n
End Sub

The MsgBox dialog box appears with the 'vbInformation' dialog type and the title text is '"Result".

The list of dialog types is shown in the Quick Info text upon entering the Prompt text.

The attached projects InputBoxMsgBox01 and InputBoxMsgBox02 demonstrate the use of input boxes to produce a message box with the results.

Example

The following program shows how the MsgBox function returns value.

Private Sub Command1_Click()
    n = MsgBox("hello", vbOKCancel)
    If n = 1 Then
        Print "You have pressed ok"
    ElseIf n = 2 Then
        Print "You have pressed cancel"
    End If
End Sub

When you click the button, the MsgBox dialog appears. Then the user either presses Ok or Cancel in this case. The MsgBox function returns 1 if you press Ok, it returns 2 if you press Cancel.

Line continuation character( _)

In your vb program you can type maximum of 1023 characters in a line. And sometimes, it doesn't fit in the window when we write so many characters in a line. So Line Continuation Character (underscore preceded by a space) is used to continue the same statement in the next line.

Example

Private Sub cmdShow_Click()
    MsgBox "Hello, welcome to www.vbtutes.com", _
   vbInformation, "Welcome"
End Sub

Output using PictureBox

PictureBox is also used for the output operation. The output is displayed using the Print method.

Example

Private Sub cmdDisplay_Click()
    Dim n As Integer
    n = 40
    picResult.Print n + 5
End Sub

The program prints 45 in the PictureBox.

The attached project PictureboxOutput shows how to show messages in PictureBox. The Print and Cls methods are used in this program.

Input-output with File

Input-output operations are also possible using File in Visual Basic discussed in the appropriate lessons. In this case, the input is taken from the data saved in Windows Notepad , in .txt extension and output can be displayed in the same file.

Output to the printer

The output can be displayed using printer . Visual Basic easily lets you display the output value or the output message using printer. Visual Basic treats the printer as an object named Printer. This topic is discussed in the appropriate lesson.

Looking for the more of the VB6 Tutorial? Here is the tutorial index.


About the Author

VB6Boy has posted a total of 71 articles.


Comments On This Post

No comments on this post yet!


Do you have a thought relating to this post? You can post your comment here. If you have an unrelated question, you can use the Q&A section to ask it.

Or you can drop a note to the administrators if you're not sure where you should post.


Your IP address is:23.20.165.182

Before you can post, you need to prove you are human. If you log in, this test goes away.




Code Links