Press "Enter" to skip to content

VB – Working with TextBox and Labels

        This Visual Basic tutorial describes how to programmatically change or edit the text of Textbox and Label

In previous lesson, we have learnt the basics of Visual basic and C# which contains how to create a new project and how to add tool elements into the window form. So we could add textboxes, label, buttons etc. but we have not programmed anything yet.

Let us start to write our VB program.

First of all, you have to understand that, we have to write our VB codes according to events.

What is an event? 

An event is an activity commonly caused by user or programmatically.
For example, if user clicks on a button, then that is an event.

Some of examples for event:

  • Button Double click
  • Key press by keyboard
  • Mouse Click
  • Form Load
  • Mouse Move
  • List box Item Selected
  • Combo box item Selection changed
  • etc.

So we have to write our VB code inside these events.

In previous lesson we created a Label (Name) and a TextBox.

The textbox has no value right now. So we are going to add a text on the textbox using our VB code.
We are going to add our code on form load event.

What is the Form load event:

This event is the activity when the form is loaded at first time. So if we add our VB code inside this event, then the code works whenever load the form.

How to open form load event:

It is very simple to open events on VB. You just double click on the form header or any blank space on the form.

VB taking form load event

Now you will get a code space like this:

VB form load event code

You have to write your VB codes inside this event code lines.

Before writing our codes, you must know the name of your tool elements such as Textbox and Label.

How do we get the elements’ name? 

We have added a label and a TextBox. To get the name of textbox, then go to its properties window (we have leant already how to get the properties window in our first lesson).

 

  • As we seen the text of label1 in first lesson, just look up for Name.
VB Textbox name property
  • Now it is ‘TextBox1’. We can change the name of any element. I just renamed into ‘username’.
Changing VB textbox name

Now your VB program knows your Textbox1 in the name of ‘username’.

Currently there is nothing inside the username textbox. So we are going to add some text inside it at form load event.
So we are going to write our first VB code.

username.Text = www.yourowncodes.com
programatically changing textbox text in vb

 

Code Explanation:

username.Text   means the Text on the textbox ‘username’.

= www.yourowncodes.com means the text on the textbox ‘username’, which is assigned as ‘www.yourowncodes.com’.  The symbol ‘=’ is used for assigning values.

Here we added a double quotes (single quote also works) because the text ‘www.yourowncodes’ is an unknown string to the VB. If we are going to set some numbers, then no need of any quotes.
Example: username.Text = 500

Let’s run the application. Now the value inside the textbox ‘username’ should be changed.

changing textbox text value in VB

While coding, you will automatically suggested some program codes. So always use these intelliSense suggested codes to avoid errors. Keyboard key ‘Tab’ is used to get the suggested keyword.

VB suggestion intellisense

Like this you can change the text on the Label. Here we already set the text on the label1 into ‘Name’. We can also change its text using programmatically.
So first of all we have to find the name of the label. That may be Label1.

VB Label name properties

Then the code  Label1.Text = “Your Site”  Will change the text on the Label1 from ‘Name’ into “Your Site”.

VB change Textbox Label Text output
Now the final VB Codes:
Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        username.Text = “www.yourowncodes.com”
        Label1.Text = “Your Site”
    End Sub
End Class

Be First to Comment

Leave a Reply