Working with TextBox Text and Label Text in VB
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’.