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
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.
Now you will get a code space like this:
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?
- As we seen the text of label1 in first lesson, just look up for Name.
- Now it is ‘TextBox1’. We can change the name of any element. I just renamed into ‘username’.
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
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.
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.
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.
Then the code Label1.Text = “Your Site” Will change the text on the Label1 from ‘Name’ into “Your Site”.