Lesson 5: Working with Controls in VB2019

Learn to effectively use TextBox and Label controls to create interactive applications

Key Takeaway

Mastering the use of controls like TextBox and Label is fundamental to creating interactive Visual Basic applications. This lesson teaches you how to accept user input, display results, and create practical applications like calculators.

In Visual Basic 2019, controls are the building blocks of your application's user interface. Understanding how to work with these controls programmatically is essential for creating functional applications. This lesson focuses on two essential controls: TextBox and Label.

5.1 Understanding VB2019 Controls

Visual Basic 2019 provides a wide variety of controls that you can use to build applications. These controls are organized into several categories in the Toolbox:

Common Controls

Buttons, labels, textboxes, and other frequently used UI elements

Containers

GroupBox, Panel, TabControl for organizing other controls

Menus & Toolbars

MenuStrip, ToolStrip for application navigation

Data Controls

DataGridView, BindingSource for database interaction

Visual Basic 2019 Controls
Figure 5.1: Visual Basic 2019 Controls in the Toolbox

5.2 The TextBox Control

The TextBox control is essential for accepting user input and displaying output. It can handle both string and numeric data, making it versatile for various applications.

TextBox Properties

Property Description
Text The text content displayed in the TextBox
Multiline Enables multiple lines of text
PasswordChar Displays a character instead of actual text for passwords
ReadOnly Prevents user from modifying the content
MaxLength Maximum number of characters allowed

Example 5.1: Simple Addition with TextBox

This example demonstrates how to use TextBox controls to accept numeric input and display the result in a message box.

TextBox Addition UI
Figure 5.2: Addition Application UI
MessageBox Result
Figure 5.3: Addition Result in MessageBox

The code behind the Add button:

Form1.vb
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ' Convert text input to numbers and calculate sum
    Dim num1 As Single = Val(TextBox1.Text)
    Dim num2 As Single = Val(TextBox2.Text)
    Dim sum As Single = num1 + num2
    
    ' Display the result in a message box
    MsgBox("The sum is " & sum)
End Sub

Output:

Important Notes

Val() Function: Converts a string to a numeric value. Essential when working with TextBox inputs for calculations.

String Concatenation: The & operator combines strings and numeric values for display.

5.3 The Label Control

Labels are primarily used to display static text, provide instructions, or show output. Unlike TextBoxes, users cannot edit Label content directly.

Label Properties

Property Description
Text The text displayed on the label
AutoSize Automatically adjusts size to fit content
TextAlign Alignment of text within the label
BorderStyle Adds a border around the label
BackColor Background color of the label

Example 5.2: Simple Calculator with Label

This example creates a calculator that adds two numbers and displays the result in a Label control instead of a message box.

Calculator UI with Label
Figure 5.4: Calculator Application UI

The code behind the Calculate button:

Form1.vb
Public Class Form1
    Private Sub BtnCal_Click(sender As Object, e As EventArgs) Handles BtnCal.Click
        ' Declare variables to store numbers
        Dim num1 As Single = Val(TxtNum1.Text)
        Dim num2 As Single = Val(TxtNum2.Text)
        
        ' Calculate the sum
        Dim Sum As Single = num1 + num2
        
        ' Display the result in the label
        LblAns.Text = Sum.ToString()
    End Sub
End Class

Output:

Important Notes

Variable Declaration: The Dim keyword declares variables to store numeric values.

ToString() Method: Converts numeric values to strings for display in the Label control.

Naming Conventions: Notice the use of prefixes (Txt, Lbl, Btn) for control names to make code more readable.

Interactive Calculator Preview

Try this interactive calculator simulation to understand how the controls work together:

First Number:
Second Number:
Result will appear here

Lesson Summary

In this lesson, you've learned how to effectively work with TextBox and Label controls:

TextBox Control

Accept user input and display output using TextBox controls

Label Control

Display static text, instructions, and calculation results

Numeric Conversion

Use Val() to convert text input to numeric values

Practical Application

Create a functional calculator using TextBox and Label controls

You've learned how to work with two fundamental controls to create interactive applications. In the next lesson, we'll explore more advanced controls: ListBox and ComboBox.

Next Lesson

Ready to learn about list-based controls? Continue to Lesson 6: ListBox and ComboBox.

Related Resources

VB6 Tutorial

Mastering VB6 Programming

Explore Tutorials

Visual Basic Examples

Practical VB code samples for real-world applications

View Examples

Excel VBA Tutorial

Learn how to automate Excel by creating VBA macros

Learn More

VB2019 Paperback

Comprehensive guide to Visual Basic 2019

Get on Amazon