Hello all.. This page will help you learn basics of visual basic programming
 
Take a VB.net quiz now
 
1. Desigining a simple login form in VB.Net
Here, we will create a basic Login form using which one can login to the system after feeding true credentials.
The form should throw error message if the username or passwod were not fed correctly.
The input fields can't be blank
The password fields should not show characters entered.
 
 
Login
Do you have a website? Increase traffic of your website. Submit your website to most of the search engines
Submit ExpressSearch Engine Marketing Services
VB.Net provides a very easy to use development environment where one can develop a basic to full-fledged windows application easily.
 
Before starting this tutorial, make sure you have installled following things
  1. Microsoft visual studio 2008
  2. MSDN (offline help library)
     Now open the Microsoft visual studio 2008 or other latest versions.
 
First Session:
 Design Part:
  1. Go to File-> New Project
  2. Select "Windows application" and give this project a name and click next
  3. By default a Form will be created with the title "Form1",
  4. Select  the form and go to properties. A properties table will be there at the right side
  5. Change the name to "Log On"
  6. On the left panlel, a tollbox is there which contains all the tools that can be used on a windows form. Eg. Button, Textbox, Checkbox, radio button, frames, images, labels etc.
  7. Now, drag a Button from the toolbox and drop it on the form where you want it to be. You can double click also on the button icon in the toolbox. The buttton will be added to your form. You will have to palce it correctly, then.
  8. Select the button1 and go to the properties panel at the right. Change the Text from "Buttton1" to "Log On"
  9. Like wise drag one more button from toolbox. You can copy the existing button of your form and paste there. A new button will be added with name "Button"
  10. Now you have to get some lables, textboxes and set the properties as per below table.
     
Controls
Default text
New Value
Name
Button
Button1
Log On
btnLogin
Button
Button2
Cancel
btnCancel
Label
Label1
User Name
lblUser
Label
Label2
Password
lblPassword
TextBox
 
 
txtUser
TextBox
 
 
txtPassword
Label
Label3
Welcome! Please Enter your user name and password below.
 
PictureBox
 
Browse a picture
picLogin
Form
Form1
Log On
frmLogin
 
  1. Now select the password textbox "txtPassword" and go to properies panel. Set the     password character to "*" o "&" . Now, when a character will be entered into this field, it will be displayed as password character "*".
  2. Set the height and width of your window form to make it smaller as per the figure. Also set the resize value to "false". The form can be resiezed now.
  3. If you want you can test your application. Build and run it. The form will appear, but no buttons will work.
Coding Part
  1. Now we will add a little code on the button we have created. Coding in VB.Net is fun
  2. Just double click the "Log on" button. You will enter the coding part. Here you have to enter the code that will be executed after clicking this button.
  3. Enter these lines on the button "Log On" code:

if txtPassword.text.length<>0 && txtUser.text.length<>0 then

'checks if the some values are entered into the textboxes
if txtPassword.text="shankar" && txtUser.text="kumar"

then messagebox.show "Login successful!!"
else
' if the username not entered as "shankar" and password not
'as "kumar", it will say: incorrect
messagebox.show "Sorry,User Name or password is incorrect!!"
'clearFields()
end if
else
messagebox.show "Neither of fields can be blank!!"
end if

On Cancel  button put below code.

frmLogin.close()
'to simply close the login window.
 
You are done!!
Now build and run this application.
 
Additinally you can uncomment the "clearFields()" and put below code for it.
 
public sub clearFields()
txtPassword.text=""
txtUser.text=""
end sub
To comment a line in VB.net just prefix it with " ' " .
 
 

2.Designing a calculator in VB.net

This tutorial will help you to build a windows calculator from scratch We will see stepwise from basic to create this application 1. Open your visual Stdio 2008 or newer, Create a new Project, select windows application, give it a name

Calculator img1

2.Now you will see a form by default. select the form and go the properties.
Set the text propery of form to "Calculator". It will give a title to your calculator

calculator img2
 
3. Now select a textbox from the toolbox and place it on your form. You can do it
by double clicking on the textbox in the toolbox or you can drag the textbox and
drop it on top of the form.
4. Set text alignment of the textbox to "right". 
5.Now place as many buttons as per the requirement. buttons for digits(0-9), (+,-,*,/, = ,.) and others.
Set properties of the buttons as shown in the below table.
Control Type Initial Value New Text Name Other Property
Button Button0 0 btnkey0  
Button Button1 21 btnkey1  
--- --- --- ---  
Button Button9 9 btnkey9  
Textbox textbox1 0 txtdisplay Text-align: Right
Button Button10 + btnPlus  
Button Button11 - btnMinus  
Button Button12 * btnMult  
Button Button13 / btnDiv  
Button Button14 / btnEqual  
Button Button15 / btnBack  
Button Button16 / btnDecimal  
6.Now Lets do some coding. Double click on the button with text "1". You will enter into the code section for this button. Here we have to put code for what we want to do when we click this button.
The text on the textbox should change to 1 if its blank and 1 should be apended to the numbers if the display textbox is not blank. So put below code on this.
 
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        txtdisplay.Text += btnkey1.Text  ' The button text '1' will be appended to the display textbox.
 End Sub
Note:- Private Sub---- End Sub are generated by just double clicking on the button. Just put the code in between.
7.Do the same for all the buttons(0-9), also same for decimal button.
See the figure below.
8. Now coding for +,-,*,/  buttons. Declare some global variables at the top of your code section.
Public Class Form1
    Dim b As Double  ' To store the values of numeric buttons for +,-,*,/
    Dim a As Double  ' To store the values of numeric buttons for +,-,*,/
    Dim oper As String
9. Go to coding for the plus button.
Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
        operation = "+"  ' sets the oper string to "+" that means addition is to be done
        a = Val(TextBox1.Text) ' a will store the first number from the textbox
        TextBox1.Text = ""  ' and the textbox will be blank again to get 2nd number
    End Sub
Do the same for other operations: minus, multiply, divide. Only change is set operation="-"  for minus button and so on..
10.Now we will see result by coding for "=" button. Put below code on this button.
  b = Val(TextBox1.Text)  ' will store the 2nd number from textbox
        If oper.Equals("+") Then   'operation' is set to "+" the textbox will display a+b
            TextBox1.Text = a + b   
        ElseIf oper.Equals("-") Then ' same with others
            TextBox1.Text = a - b
        ElseIf oper.Equals("*") Then
            TextBox1.Text = a * b
        ElseIf oper.Equals("/") Then
            TextBox1.Text = a / b
        End If
calculator img3
11. We need a back button also to clear the textbox one by one if user enters any wrong digit.
So, think how you will make 123 to 12 or 5435 to 543 or 645 to 64...
The logic is divide the number by 10, subtract the number by remainder and then divide the number by 10 and put the result into display. See code.
calculator img4
 
        Dim rema, nval As Double
        rema = Val(TextBox1.Text) Mod 10
        nval = Val(TextBox1.Text) - rema
        TextBox1.Text = nval / 10.0
12. Additionally You can put a square root button, reciprocal(1/x) button, percentage button(%) also. See code for the same.
 
Private Sub btnReciprocal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
        TextBox1.Text = 1.0 / Val(TextBox1.Text)
    End Sub
 
    Private Sub btnSqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button20.Click
        TextBox1.Text = Math.Sqrt(Val(TextBox1.Text))
    End Sub
 
    Private Sub btnPercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button21.Click
        b = Val(TextBox1.Text)
        Dim c As Double
        c = a * b
        TextBox1.Text = Val(c / 100.0)
    End Sub
 
 13) To get the square of the number, you can try below code:
 
Private Sub btnSquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button22.Click
       TextBox1.Text = Val(TextBox1.Text)*Val(TextBox1.Text)
End Sub