Commonly Used Controls in VB.NET June 26, 2008
Posted by prasu in : Technical, Tools, VB.NET , 1 comment so far- Start a New Project or select an existing Project from the Start Page
- Add a New Windows Form to your Project
To do this-
Click on Project—>Add Windows Form —> Select Windows Form
Give any name that you desire to the form.
-
Make sure your Toolbox and Properties palette are visible.
- Now, drag a Label,Textbox and a Button from the Toolbox to the Form.
-
Select the ‘Label1’ by clicking it once, go to the Properties palette and clear the text from the Text property. Similarly change the Text property of the ‘Button1’ as Enter.
-
As mentioned, VB.NET is about event programming. Let us write some code for the ‘Form Load’ event. This code will be executed as soon as the Form is opened. To do so, double click anyhere inside the Form (Aliter: Rt Click -> View Code)
It will take you to the code window with the following code already placed-
- The cursor will be blinking inside the method Form2_Load. By now you would have figured out that this is where we need to code for the Form Load event.
- Now write the following code inside the method-
- This line is used to disable the button1 control. i.e the Button will be visible but it cannot be clicked.
- Now go back to the Design Window for Form2 (from the Tabs at the top of the page) and double click on the Textbox
Now you will be taken to the code window and the cursor will be blinking inside the method Textbox1_TextChanged
- The default event for a Textbox is the ‘Text Changed’ event. This can of course be changed. To view all the events associated with a Control and code for other events, you can see a dropdown at the top-right in the Code Window. Now write the following code for our Textbox:
- ‘Me’ refers to the current form (analogus to ‘this‘ in Java).
me.textbox1.text.length gives the length of the string typed in the Textbox.We are going to make the Button clickable (enabled) only if some text exists inside in the Textbox.
- Go back to the design window and then double click on the Button you will be taken to the method Button_Click.
Now write the code:
- In the above code we are setting the Text property of the Label programmatically.
Any string is to be written inside the quotation marks.
‘&’ is the concatenation character
vbCr is the new line character in VB.NET
- To summarize, we show here the complete code:
- Now Debug the program by clicking on the green run button (F5)
-
Now write some text in the Textbox and click on the Button
For Example I have written ‘Welcome to VB.net’.

Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Me.button1.Enabled = False
If Me.TextBox1.Text.Length > 0 Then Me.button1.Enabled = True End If
Me.Label1.Text = "The text is " & vbCr & Me.TextBox1.Text & vbCr & "The length is " & Me.TextBox1.Text.Length
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.button1.Enabled = False
End Sub
Private Sub button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles button1.Click
Me.Label1.Text = "The text is " & vbCr & Me.TextBox1.Text & vbCr & "The length is " & Me.TextBox1.Text.Length
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If Me.TextBox1.Text.Length > 0 Then
Me.button1.Enabled = True
End If
End Sub
End Class


Note:
The above message can also be made to show in a Message Box (’Hello World’ example like)
For that in the method button_click instead of using
Me.Label1.Text = "The text is " & vbCr & Me.TextBox1.Text & vbCr & "The length is " & Me.TextBox1.Text.Length
The following code has to be used
Msgbox("The text is " & vbCr & Me.TextBox1.Text & vbCr & "The length is " & Me.TextBox1.Text.Length)
You have now learnt some commonly used Controls in VB.NET

