The TextBox

Text boxes are used for entering and displaying data.

The property you will use most commonly is the Text property.

One of the commonest methods you will use is Focus, which positions the cursor inside the text box.

If you had a textbox named txtName, then the code below would display whatever has been typed into it in a label named lblName, clear its contents by setting its Text property to blank (“”), and finally put the cursor in the text box ready for the next piece of data to be typed in.

lblName.Text = txtName.Text

txtName.Text = ""

txtName.Focus()

Variables

When a program runs, any data that it uses must be stored in RAM. A variable is a name made up by the programmer to identify the address in RAM where a particular piece of data is stored. Our programs so far have managed to avoid using variables but this restricts what can be done.

For example, the code

lblNumber.Text = txtNumber.Text

displays in the label lblNumber whatever the user has typed into the textbox txtNumber.

But to store what is in the textbox and use what is stored after the user has typed something else into the textbox, we would need a variable.

Data types

Every variable must be of a given data type for example Integer, String, Decimal. Full list can be found on page 64-65

Declaring variables

To declare a variable means to tell Visual Basic two things about it:

· Its name or identifier.

· Its data type.

The Dim statement

Use the keywords Dim and As when you declare a variable. The code below declares three variables

of different data types.

Dim intNumber As Integer

Dim decPayment As Decimal

Dim dtDateOfPayment As Date

Note:

· Always use meaningful identifiers. The three variables could have been declared as A, B and

C but these would tell you nothing about what they are used for storing.

· Identifiers are always prefixed with (usually) three letters indicating their data type. For

example, intNumber is an Integer and has the prefix “int”, decPayment is decimal and has the

prefix “dec” and so on.

· Identifiers cannot have spaces. When you use two or more words you could join each by an

underscore, for example date_of_payment or you could omit the underscores and make each

start with an uppercase letter, eg DateOfPayment. This is called I-capping. It is industry

standard to use I-capping for variables and underscores for constants.

You can declare more than one variable of the same data type in the same line of code. For example

Dim intFirstNumber, intSecondNumber, intThirdNumber As Integer

Methods of displaying and formatting output

Visual Basic .NET does not support this but there are other ways of displaying output on the

screen:

Label

A label is useful when you need to display something short and simple, such as the result of

adding some numbers, the current time or a text message of several words. Although you can

display more than one line of output in a label, it is not a sensible means of displaying many

lines because you cannot see any output that does not fit into it.

Text box A text box displays one line only, but if its MultiLine property is set to True, it can display two

or more lines. Unlike a label you can view any hidden display by using the up and down arrow

keys. Set a text box’s ReadOnly property to True if you use it for output since the user should

not be able to change it’s content.

List box A list box is the most versatile basic control for displaying output. Use its Items.Add method to add new lines. Vertical and horizontal scroll bars are provided automatically if needed.

Message box Message boxes are for giving users brief messages such as Correct or Incorrect, or to give them a choice of doing something by having Yes or No buttons. You can also display text

directly in a message box, although you would not normally use it to display a lot of data.

Concatenating strings

Sometimes you need to concatenate or join together two or more pieces of data together and output them as a single longer string.

Use the ampersand or concatenation operator &, which we used in Week 3. The code below concatenates two names into one.

It uses three ampersands to concatenate four items of data, two literal strings (in quotation marks) and two variables.

The third item of data, the “ “, is a single space to separate the two parts of the name.

If strFirstName stores Jane and strSurname stores Hughes then the output would be Your name is Jane Hughes.

strSurname = txtSurname.Text

strForename = txtForeName.Text

lblFullName.Text = "Your name is " & strForename & " " & strSurname

Literal strings to concatenate

"Your name is " & strForename & " " & strSurname

Variables to concatenate