Lesson 5

Working with Controls

In this lesson, you will learn how to use some of the most common controls in Visual Basic 2015, especially the TextBox and the Label. These controls are widely used in form-based applications because they allow the user to enter data and allow the program to display results.

Lesson focus:

A TextBox is commonly used for input, while a Label is commonly used for instructions or output. Once you understand how these two controls work, you can build many simple yet useful programs.

Lesson Overview

Lesson5
TopicWorking with Controls
Main ControlsTextBox and Label
Example AppSimple Calculator
Next StepListBox and ComboBox
5.1 TextBox Input
5.2 MsgBox Output
5.3 Label Output
5.4 Simple Calculator

Using Common Controls in Visual Basic 2015

Visual Basic 2015 provides many useful controls such as the label, text box, button, list box, combo box, picture box, timer, and more. In this lesson, we focus on two of the most important ones: the TextBox and the Label.

A TextBox is commonly used to accept input from the user. It can also display output, though it is usually used for input. Because the content of a text box is stored as text, you often need to convert it into a numeric value before performing calculations.

One common way to convert text into a number in Visual Basic is to use the Val(...) function.

  • TextBox is usually used for input.
  • Label is usually used for instructions or output.
  • Val(Text) converts text into a numeric value.
  • MsgBox can be used to display the result quickly.

Adding Two Numbers and Showing the Result in a Message Box

In the first example, the program adds the values entered in two text boxes and then displays the sum in a message box. This is one of the simplest ways to build an input-output application.

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    MsgBox("The sum is " & Val(TextBox1.Text) + Val(TextBox2.Text))
End Sub

The code above does three important things:

  • It reads the value from TextBox1.
  • It reads the value from TextBox2.
  • It uses Val(...) to convert the text into numbers before adding them.

Without using Val(...), Visual Basic would treat the textbox values as text strings, not as numbers.

VB2015 Figure 5.1 Input form for adding two numbers

Figure 5.1: Input form for adding two numbers

VB2015 Figure 5.2 Sum displayed in a message box

Figure 5.2: Sum displayed in a message box

Using a Label to Display Output

A label in Visual Basic 2015 can be used to provide instructions to the user and also to display output. It is different from a text box because the user cannot edit its content directly during program execution.

By using the syntax Label.Text, you can display text or numeric data on the form. You can also change the label text through the Properties window during design time or through code at runtime.

  • Labels are useful for headings and instructions.
  • Labels can display calculated results.
  • Labels are not meant for user input.
  • You can style labels by changing color, font, size, and background.

Displaying the Sum on a Label

In the next example, instead of displaying the sum in a message box, we display the answer directly on a label. This creates a cleaner and more user-friendly interface.

To build this simple calculator, place two text boxes, three labels, and one button on the form. Then set the properties as follows:

  • Change the name of the first textbox to TxtNum1.
  • Change the name of the second textbox to TxtNum2.
  • Set the first label text to +.
  • Set the second label text to =.
  • Set the third label name to LblAns.
  • Set AutoSize of the third label to False.
  • Resize the third label and set its background color to yellow.
  • Change the button name to BtnCal.
  • Change the button text to Calculate.
  • Change the form text to Simple Calculator.

The text boxes accept the user's numbers, and the label named LblAns shows the result.

Calculator Code Using Variables and a Label

Private Sub BtnCal_Click(sender As Object, e As EventArgs) Handles BtnCal.Click
    Dim num1 As Single = Val(TxtNum1.Text)
    Dim num2 As Single = Val(TxtNum2.Text)
    Dim Sum As Single = num1 + num2
    LblAns.Text = Sum.ToString
End Sub

This example introduces a few new ideas:

  • Dim is used to declare variables.
  • Single means a single-precision numeric type.
  • Val(...) converts text input into a number.
  • ToString converts a numeric value back into text for display.

You do not need to master variable declarations fully at this stage because a later lesson will explain them in more detail. For now, it is enough to understand that the program reads input, stores the values, performs the addition, and then displays the result on the label.

VB2015 Figure 5.3 Simple calculator output

Figure 5.3: Simple calculator output shown on the label

Why This Lesson Matters

Core takeaway:

TextBox and Label are two of the most widely used controls in Visual Basic programming. Once you know how to collect input from a textbox and show output in a label or message box, you can already build useful mini-applications such as calculators, converters, and form-based utilities.

Build on This Foundation

Continue to VB2026

After learning the basics of working with controls in VB2015, move to the newest VB2026 tutorial for a more modern VB.NET learning path.

Explore VB2026 →

Visual Basic Programming

Visual Basic Programming

Use this Top Release book to reinforce your tutorial learning with a more structured guide.

Exercise Questions

  1. What is the difference between a TextBox and a Label in Visual Basic 2015?
  2. Why do we use the Val(...) function when working with textbox input?
  3. Modify the calculator example so that it subtracts the second number from the first number.

Go to Lesson 6

In the next lesson, you will learn how to work with two more useful controls: the ListBox and the ComboBox.