Variable Declaration in Visual Basic .Net

Variable Declaration in Visual Basic .Net

Tutorial Description:

This is a basic tutorial not just only in vb.net but also in other programming languages. This is on declaring of variables. It is gui (graphical user interface) program in vb.net that ask the user to fill-up the fields or textboxes on the form.

What we need?

We need a visual basic .net installer or the visual studio 2010 or higher versions of visual studio.

Let’s start the tutorial

1. Open the Microsoft Visual Studio 2010 or higher.

2. Create a new project. Click File then select New Project.

3. Select Visual Basic > Windows Form Application, click OK.

4. We need the following controls:
2 labels
2 textboxes
1 checkbox
1 button

vbnetp1

The above image will be the layout of the form.

5. Double click the second textbox (the one with the caption of Enter your age (integer)) and paste the code below.

If IsNumeric(TextBox2.Text) = False Then
TextBox2.Text = ""
End If

Code explanation: the code above will prevent the user from entering a non-numeric value.

Note: when you double clicked the textbox the default event showed is the TextChanged event, which means that the code above will trigger whenever the user has something to input on the textox.

To make sure, here’s the full code:

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
If IsNumeric(TextBox2.Text) = False Then
TextBox2.Text = ""
End If
End Sub

6. Next is double click the button and paste the code.

Dim myName As String = TextBox1.Text
Dim myAge As Integer = CInt(TextBox2.Text)
Dim myBoolean As Boolean = CheckBox1.Checked
MessageBox.Show(myName & ", " & myAge & ", " & myBoolean)

Note: this code will execute whenever the user clicks the button.

Code explanation:

We have declared 3 variables namely: myName as string type, myAge as integer type and myBoolean as Boolean type. The value of the textboxes and checkbox will be passed to the variables and will be displayed in a message box.

7. Save the Project and Execute the program (same with vb6 press F5)

Happy Programming and stay tuned, more tutorials to come. Thank you.
Download

, , ,

Post navigation