VB Input/Output

Introduction
In visual basic, data can be input by initialization, assignment, through objects or via an input box. This data can be of any visual basic data types (Integer, Currency, String e.t.c)

Input Box
This is a user prompt (dialogue box) that appears on the user screen and contains a title bar, a descriptive message to the user a text area and command buttons (e.g OK and Cancel).

OK - When clicked it inputs the entered value into the program and offloads the input box from the screen.

Cancel - When clicked, it discards the value entered and offloads the input box from the screen.

Syntax: VariableName = InputBox("Descriptive Message", "Title") Example: StudName = InputBox("Enter Students Name", "Student Name")

Sample Code: private sub calculate_KeyPress(keyAscii as Integer) on Error Resume Next Dim x, score(1 to 10), total As Integer total = 0 for x = 1 to 10 step 1 score(x) = InputBox("Enter value for score" & x)  total = total + score(x) next x print"The Total score =; total: end sub

Controls
Visual basic also allows the user to extract data from a Control (e.g text box or list box) into the program. When doing this, you need to know the name of the control you are extracting data from and the name of the variable you are saving the data to.

Syntax: VariableName = ControlName Example: StudName = txtStudent.Text Sample Code:

Extracting data from an InputBox to a Control
Visual Basic allows the use of the InputBox to obtain data and the insertion of the data into a Control.

Syntax: control = InputBox("Message", "Title") Example: txtStudent = InputBox("Enter Student Name", "Student Name") Sample Code: Private sub cmdDetails_Click txtStudent = InputBox("Enter Student Name", "Student Name") End Sub

VB Output
The print statement is used to output data in a Visual Basic application. Syntax: print VariableName print "Descriptive text"; VariableName Print - is a key word. The descriptive text is not meant for the program but is to be displayed to the user and therefor must be enclosed in quotes(" "). VariableName - This is the name of the variable that is declared in the program (controls can also be used)

Outputting data Via Controls
Picture Box: In visual basic you can output data to the picture box. To do this, you need to know the name of the picture box.

Syntax: picturebox.print "Descriptive text", VariableName Example 1: picDisplay.Print "Form Caption is", frmDetails.Caption The above example outputs the caption property of a form via a picture box.

To output data via a text box see Extracting data from an InputBox to a Control

List Box and Combo Box The procedure for adding data to a List Box and Combo Box is the same. A list box can allow many lines of data to be populated to it. First, you need to know the name of the list box.

Syntax: ListBoxName.AddItem ItemName ListBoxName: This is the name of the list Box. AddItem: It is a predefined method. ItemName It is the name of the Variable or String to be added. If you want to add a string, then the string must be enclosed in quotes.

Example of adding strings to a list box: lstCountry.AddItem "Kenya" Example of adding strings to a Combo box: cboCity.AddItem "Nairobi" cboCity.AddItem "Mombasa" Sample code 1: Private sub cmbItems_Click 'The statement to add a Labels caption to a combo box cmbItems.AddItem lblStudName.Caption End sub Sample code 2: Private Sub Form_Click 'Populate combo box with potential city names cboCity.AddItem "Nairobi" cboCity.Text = "Nairobi" cboCity.AddItem "Mombasa" cboCity.AddItem "Nakuru" cboCity.AddItem "Malindi" cboCity.AddItem "Nyeri" cboCity.AddItem "Thika" cboCity.AddItem "Kitale" End Sub