Sunday, February 5, 2017

Simple Calculator In VB Try it......

 
Calculator Program

Here is a Code for  for making simple calculator in VB. You can try it in your projects for practice. It is very simple and work good. You can analyze and review the code for your further projects.

You Need to first drag the necessary controls to the form and make design as shown in the picture.




Then here is a code for the calculator ....

--------------------------------------------------------------------------------


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles comadd.Click
        If Label1.Text <> 0 Then
            txt1.Text = Label1.Text
            Label4.Text = "+"
            Label1.Text = "0.00"    continue.

            txt2.Text = ""
            txt2.Focus()
        Else
            Label4.Text = "+"
            txt1.Text = txt2.Text
            txt2.Text = ""
            txt2.Focus()
        End If
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Label4.Text = ""
        txt1.Enabled = False
    End Sub
    Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
        txt1.Text = ""
        txt2.Text = ""
        Label4.Text = ""
        Label1.Text = "0.00"
        txt2.Focus()
    End Sub

    Private Sub cmdsub_Click(sender As Object, e As EventArgs) Handles cmdsub.Click
        If Label1.Text <> 0 Then
            txt1.Text = Label1.Text
            Label4.Text = "-"
            Label1.Text = "0.00"
            txt2.Text = ""
            txt2.Focus()
        Else
            Label4.Text = "-"
            txt1.Text = txt2.Text
            txt2.Text = ""
            txt2.Focus()
        End If
    End Sub

    Private Sub cmdivide_Click(sender As Object, e As EventArgs) Handles cmdivide.Click
        If Label1.Text <> 0 Then
            txt1.Text = Label1.Text
            Label4.Text = "/"
            Label1.Text = "0.00"
            txt2.Text = ""
            txt2.Focus()
        Else
            Label4.Text = "/"
            txt1.Text = txt2.Text
            txt2.Text = ""
            txt2.Focus()
        End If
    End Sub

    Private Sub cmdmulti_Click(sender As Object, e As EventArgs) Handles cmdmulti.Click
        If Label1.Text <> 0 Then
            txt1.Text = Label1.Text
            Label4.Text = "x"
            Label1.Text = "0.00"
            txt2.Text = ""
            txt2.Focus()
        Else
            Label4.Text = "x"
            txt1.Text = txt2.Text
            txt2.Text = ""
            txt2.Focus()
        End If
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        If txt2.Text = "" Then
            txt2.Text = "1"
        Else
            txt2.Text = txt2.Text + "1"
        End If

    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        If txt2.Text = "" Then
            txt2.Text = "2"
        Else
            txt2.Text = txt2.Text + "2"
        End If
    End Sub

    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        If txt2.Text = "" Then
            txt2.Text = "3"
        Else
            txt2.Text = txt2.Text + "3"
        End If

    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        If txt2.Text = "" Then
            txt2.Text = "4"
        Else
            txt2.Text = txt2.Text + "4"
        End If
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        If txt2.Text = "" Then
            txt2.Text = "5"
        Else
            txt2.Text = txt2.Text + "5"
        End If
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If txt2.Text = "" Then
            txt2.Text = "6"
        Else
            txt2.Text = txt2.Text + "6"
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        If txt2.Text = "" Then
            txt2.Text = "7"
        Else
            txt2.Text = txt2.Text + "7"
        End If
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        If txt2.Text = "" Then
            txt2.Text = "8"
        Else
            txt2.Text = txt2.Text + "8"
        End If
    End Sub

    Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
        If txt2.Text = "" Then
            txt2.Text = "9"
        Else
            txt2.Text = txt2.Text + "9"
        End If
    End Sub

    Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click
        If txt2.Text = "" Then
            txt2.Text = "00"
        Else
            txt2.Text = txt2.Text + "00"
        End If
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        If txt2.Text = "" Then
            txt2.Text = "0"
        Else
            txt2.Text = txt2.Text + "0"
        End If
    End Sub

    Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
        On Error GoTo err
        If Label4.Text = "+" Then
            Label1.Text = myadd(txt1.Text, txt2.Text)
        ElseIf Label4.Text = "-" Then
            Label1.Text = MYSUB(txt1.Text, txt2.Text)
        ElseIf Label4.Text = "/" Then
            Label1.Text = DIVIDE(txt1.Text, txt2.Text)
        ElseIf Label4.Text = "x" Then
            Label1.Text = MYMULTIPLY(txt1.Text, txt2.Text)
        End If
        Exit Sub
err:
        MsgBox(Err.Description)
    End Sub
--------------------------------------------------------------

There are some functions used in the projects like addition , multiply , divide etc...in the next Post i will be explaining about simple functions.


Thank you

Enjoy The Code.


Please comment if any questions.



Narayan Aryal
Butwal
Nepal

No comments:

Post a Comment