Beginning Visual Basic

Hila Science Camp

How to input data.

(This is an image of the VB form - not an active window)


There are a number of ways to enter data into a program.
Here is a simple VB program using 4 lines of code to input a name and display it.

Place a label (Label1) , text box (Text1) and button (Command1) on the Form.
Change Label1.caption to "Enter your name:" (Use properties window - press F4 if not visible)

Note that the variable "nm" has been declared as a string variable.
String variables are used to represent words.

Note the use of the "visible" propery to hide the text box and button when the button is pressed.
An apostrophe allows you to enter notes about the code, called "rems" these reminders do not affect the running of the program.
The "rems" are green in the code window.

This is an image of the VB Code window, actual code is below.

Here is the code: (copy and paste)

Dim nm As String ' use string variables for names

Private Sub Command1_Click()

    'In the properties box change Label1.caption to "Enter Your Name"
 nm = Text1.Text  ' collects input from text1, saves as variable nm

 Text1.Visible = False ' hides the text box, text1

 Command1.Visible = False  ' hides the command button

 Label1.Caption = "Hello " & nm  ' Changes label1 caption, "&" joins the variable nm

End Sub