Pages

Saturday, 13 October 2012

Design form to create calculator application in VB

Design form to create calculator application in VB

Description:

In this post we are going to learn how to make a calculator in vb 
Open Microsoft visual studio
File-> New -> Project -> visual vb ->  Windows Application

Program:

Design a window form as shown in figure in vb


Design form to create calculator application in VB
Design form to create calculator application in VB

now double click on 1 button. code file will open with this code.

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
       
    End Sub

add code 

 txtans.Text = txtans.Text + "1"

 above " End Sub " as shown below do this for all button.


Imports System.Math
Public Class Form1
    Dim s As String
    Dim a As Double
    Dim x As Double
    Dim b As Double
    Dim c As Double
    Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
        txtans.Text = txtans.Text + "1"
    End Sub

    Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
        txtans.Text = txtans.Text + "2"
    End Sub

    Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
        txtans.Text = txtans.Text + "3"
    End Sub

    Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
        txtans.Text = txtans.Text + "4"
    End Sub

    Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
        txtans.Text = txtans.Text + "5"
    End Sub

    Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
        txtans.Text = txtans.Text + "6"
    End Sub

    Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
        txtans.Text = txtans.Text + "7"
    End Sub

    Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
        txtans.Text = txtans.Text + "8"
    End Sub

    Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
        txtans.Text = txtans.Text + "9"
    End Sub

    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
        txtans.Text = txtans.Text + "0"
    End Sub

    Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
        a = Convert.ToDouble(txtans.Text)
        txtans.Text = ""
        x = 1
    End Sub

    Private Sub btnsub_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsub.Click
        a = Convert.ToDouble(txtans.Text)
        txtans.Text = ""
        x = 2
    End Sub

    Private Sub btnmul_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmul.Click
        a = Convert.ToDouble(txtans.Text)
        txtans.Text = ""
        x = 3
    End Sub

    Private Sub btndiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndiv.Click
        a = Convert.ToDouble(txtans.Text)
        txtans.Text = ""
        x = 4
    End Sub

    Private Sub btneql_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneql.Click
        b = Convert.ToDouble(txtans.Text)
        txtans.Text = ""
        Select Case x
            Case 1
                c = a + b
                txtans.Text = c
            Case 2
                c = a - b
                txtans.Text = c
            Case 3
                c = a * b
                txtans.Text = c
            Case 4
                c = a / b
                txtans.Text = c
            Case 5
                c = a * a
                txtans.Text = c
            Case 6
                c = a
                txtans.Text = c
            Case Else
        End Select
    End Sub

    Private Sub btnsquare_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsquare.Click
        a = Convert.ToDouble(txtans.Text)
        x = 5
    End Sub

    Private Sub btbdot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btbdot.Click
        txtans.Text = txtans.Text + "."

    End Sub

    Private Sub btnsqrt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsqrt.Click
        a = Convert.ToDouble(txtans.Text)
        a = Sqrt(a)
        x = 6
    End Sub

    Private Sub btnclear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclear.Click
        a = 0
        b = 0
        c = 0
        x = 0
        s = ""
        txtans.Text = ""
    End Sub

    
End Class

Demo:

Design form to create calculator application in VB
Design form to create calculator application in VB

Design form to create calculator application in VB
Design form to create calculator application in VB

Design form to create calculator application in VB
Design form to create calculator application in VB

Design form to create calculator application in VB
Design form to create calculator application in VB




No comments:

Post a Comment