In this article, you can get a basic idea about Visual basic coding. after you analyzing this you can get a good idea about the message box and variables. try to write codes after you analyzed them. Also, you can get an idea about how to use visual basic elements. like text box, command button, and labels. Let's go to the coding part.



Visual basic programming
Visual basic programming


In this program, you can calculate your water bill or electricity bill. Let's go to our program interface.

Visual basic interface
Interface


Public mNou, mTot, mPr, mCr As Integer


Private Sub cmdcalculate_Click()

mPr = Val(txtPr.Text)

mCr = Val(txtCr.Text)

mNou = mCr - mPr

lblNoofunits.Caption = mNou

If mNou < 50 Then

lblBillamount.Caption = mNou * 1

ElseIf 50 < mNou < 100 Then

lblBillamount.Caption = (mNou - 50) * 2 + 50

ElseIf 100 < mNou < 500 Then

lblBillamount.Caption = 150 + (mNou - 100) * 5

Else

lblBillamount.Caption = 150 + 400 * 5 + (mNou - 500) * 25

End If

If mCr < mPr Then

MsgBox "Invalid value", vbOKOnly + vbCritical, "Error"

lblNoofunits.Caption = ""

lblBillamount.Caption = ""

txtCr.Text = ""

txtPr.Text = ""

End If

End Sub


Private Sub cmdClose_Click()

Q = MsgBox("Are you sure", vbQuestion + vbYesNo, "Exit")

If Q = vbYes Then

End

Else: txtMno.SetFocus

End If

End Sub


Private Sub cmdNew_Click()

txtMno.Text = ""

txtMonth.Text = ""

txtCr.Text = ""

lblNoofunits.Caption = ""

lblBillamount.Caption = ""

txtPr.Text = ""

End Sub


Private Sub txtCr_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

cmdcalculate.SetFocus

End If

End Sub


Private Sub txtMno_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

txtMonth.SetFocus

End If

End Sub


Private Sub txtMonth_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

txtPr.SetFocus

End If

End Sub


Private Sub txtPr_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

txtCr.SetFocus

End If

End Sub


You can download the source file here

Also, you can refer my another VB program here