Sunday, January 1, 2012

Create a calculator in Vb.net

Example





Public Class frmCalc


    Dim tmpno, mem As Double
    Dim clear, flag As Boolean
///
    Private Sub btnnum_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) 
Handles btn0.Click, btn1.Click, btn2.Click, btn3.Click, btn4.Click,
btn5.Click, btn6.Click, btn7.Click, btn8.Click, btn9.Click
        If clear = True Or txtno.Text = "0" Then
            txtno.Text = ""
            clear = False
        End If
        flag = True
        txtno.Text = txtno.Text + CType(sender, Button).Text
    End Sub
///
    Private Sub btnc_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles btnc.Click
        txtno.Text = 0
        mem = 0
        clear = True
        tmpno = 0
        lblsign.Text = "C"
    End Sub
///
    Private Sub btnce_Click(ByVal sender As System.Object, 
ByVal e As System.EventArgs) Handles btnce.Click
        txtno.Text = 0
        lblsign.Text = "CE"
    End Sub
///
    Private Sub btnbs_Click(ByVal sender As System.Object,
                          ByVal e As System.EventArgs) Handles btnbs.Click
                                                                          
        lblsign.Text = "<-"
    txtNo.Text = Microsoft.VisualBasic.Left(txtNo.Text, txtNo.Text.Length - 1)
        If txtno.Text = "" Then
            txtno.Text = 0
        End If
    End Sub
///
    Private Sub btneql_Click(ByVal sender As System.Object,
                        ByVal e As System.EventArgs) Handles btneql.Click
        If lblsign.Text = "/" Then
            If Val(txtno.Text) = 0 Then
                txtno.Text = "Cannot devide by zero"
            Else
                txtno.Text = tmpno / Val(txtno.Text)
            End If
        End If
        If lblsign.Text = "x" Then
            txtNo.Text = tmpno * Val(txtNo.Text)
        End If
        If lblsign.Text = "-" Then
            txtNo.Text = tmpno - Val(txtNo.Text)
        End If
        If lblsign.Text = "+" Then
            txtNo.Text = tmpno + Val(txtNo.Text)
        End If
        If lblsign.Text = "%" Then
            txtNo.Text = tmpno * Val(txtNo.Text) / 100
        End If
        If lblsign.Text = "x^y" Then
            txtNo.Text = Math.Pow(tmpno, Val(txtNo.Text))
        End If


        lblsign.Text = "="
        tmpno = Val(txtNo.Text)
        clear = True
    End Sub
///
    Private Sub btn1x_Click(ByVal sender As System.Object,
                     ByVal e As System.EventArgs) Handles btn1x.Click
        txtno.Text = 1 / Val(txtno.Text)
        lblsign.Text = "1/x"
        clear = True
    End Sub
///
    Private Sub btnper_Click(ByVal sender As System.Object,
                      ByVal e As System.EventArgs) Handles btnper.Click
        lblsign.Text = "%"
        tmpno = Val(txtno.Text)
        clear = True
    End Sub
///
    Private Sub btnmc_Click(ByVal sender As System.Object, 
                     ByVal e As System.EventArgs) Handles btnmc.Click
        mem = 0
    End Sub
///
    Private Sub btnmr_Click(ByVal sender As System.Object,
                     ByVal e As System.EventArgs) Handles btnmr.Click
        txtno.Text = mem
    End Sub
///
    Private Sub btnms_Click(ByVal sender As System.Object, 
                     ByVal e As System.EventArgs) Handles btnms.Click
        mem = mem - Val(txtno.Text)
    End Sub
///
    Private Sub btnmp_Click(ByVal sender As System.Object,
                       ByVal e As System.EventArgs) Handles btnmp.Click
        mem = mem + Val(txtno.Text)
    End Sub
///
    Private Sub btnsign_Click(ByVal sender As System.Object,
                    ByVal e As System.EventArgs) 
         andles btndiv.Click, btnmul.Click, btnsub.Click, btnpls.Click
        If flag = True Then
            Call btneql_Click(sender, e)
            flag = False
        End If
        tmpno = Val(txtno.Text)
        lblsign.Text = CType(sender, Button).Text
        clear = True
    End Sub
///
    Private Sub btndot_Click(ByVal sender As System.Object,
                    ByVal e As System.EventArgs) Handles btndot.Click
        If clear = True Then
            txtno.Text = "0."
            clear = False
        End If
        If txtno.Text.IndexOf(".") <= 0 Then
            txtno.Text = txtno.Text + "."
            clear = False
        End If
    End Sub


    Private Sub btnpm_Click(ByVal sender As System.Object,
                     ByVal e As System.EventArgs) Handles btnpm.Click
        txtno.Text = -1 * Val(txtno.Text)
    End Sub
///
    Private Sub btnsqrt_Click(ByVal sender As System.Object,
                    ByVal e As System.EventArgs) Handles btnsqrt.Click
        lblsign.Text = "Sq"
        txtno.Text = Math.Sqrt(Val(txtno.Text))
    End Sub
//
   
    Private Sub CopyToolStripMenuItem_Click(ByVal sender As System.Object,
                        ByVal e As System.EventArgs)
        Clipboard.SetText(txtNo.Text)
    End Sub
///
    Private Sub PasteToolStripMenuItem_Click(ByVal sender As System.Object,
                                         ByVal e As System.EventArgs)
        txtNo.Text = Clipboard.GetText
    End Sub
///
    Private Sub numpad_KeyPress(ByVal sender As Object, 
              ByVal e As System.Windows.Forms.KeyPressEventArgs) 
Handles btn0.KeyPress, btn1.KeyPress, btn1x.KeyPress, 
btn2.KeyPress, btn3.KeyPress, btn4.KeyPress, btn5.KeyPress,
 btn6.KeyPress, btn7.KeyPress, btn8.KeyPress, btn9.KeyPress,
 btnbs.KeyPress, btnc.KeyPress, btnce.KeyPress, btndiv.KeyPress
, btndot.KeyPress, btneql.KeyPress, btnmc.KeyPress, btnmp.KeyPress,
 btnmr.KeyPress, btnms.KeyPress, btnmul.KeyPress, btnper.KeyPress,
 btnpls.KeyPress, btnpm.KeyPress, btnsqrt.KeyPress, btnsub.KeyPress
        btneql.Focus()
        If Asc(e.KeyChar) = 8 Then
            Call btnbs_Click(sender, e)
        ElseIf Asc(e.KeyChar) = 46 Then
            Call btndot_Click(sender, e)
        ElseIf Asc(e.KeyChar) = 47 Then
            Call btnsign_Click(btndiv, e)
        ElseIf Asc(e.KeyChar) = 47 Then
            Call btnsign_Click(btndiv, e)
        ElseIf Asc(e.KeyChar) = 42 Then
            Call btnsign_Click(btnmul, e)
        ElseIf Asc(e.KeyChar) = 45 Then
            Call btnsign_Click(btnsub, e)
        ElseIf Asc(e.KeyChar) = 43 Then
            Call btnsign_Click(btnpls, e)
        ElseIf Asc(e.KeyChar) = 27 Then
            Call btnc_Click(sender, e)
        ElseIf (Asc(e.KeyChar) >= 48 And Asc(e.KeyChar) <= 57) Then
            If clear = True Or txtNo.Text = "0" Then
                txtNo.Text = ""
                clear = False
            End If
            flag = True
            txtNo.Text = txtNo.Text & CChar(ChrW(Asc(e.KeyChar)))
        Else
            e.Handled = True
        End If
    End Sub


    Private Sub txtNo_TextChanged(ByVal sender As System.Object,
 ByVal e As System.EventArgs) Handles txtNo.TextChanged


    End Sub
End Class

No comments:

Post a Comment