Monday, December 16, 2013

Calculator ~~ VisualBasic Code Snippet + GUI Design


~Each button is named "btn follow by the control/tool text name". eg: button clear which is captioned 'c' is named btnc, button 0 is name btn0, button 1 is named btnone, button 2 is named btntwo............just like that. 
Note! -- except the resize event, every other line(s) of code is(are) click event.

~Still don't get it; Need the EXECUTABLE (.exe) file OR may be the design file itself; just holla at me;

----------------------------------------
Source Code ~> written with vbasic.net
----------------------------------------
Public Class Form1     ' class name

    Dim i As Double

    Dim sign As String
    Dim memory As Double

    Private Sub btnc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnc.Click

        txtdisplay.Text = 0
    End Sub

    Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click

        mthd()
    End Sub

    Private Sub btntwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntwo.Click

        mthd()
    End Sub

    Private Sub btnone_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnone.Click

        mthd()
    End Sub

    Private Sub btnthree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnthree.Click

        mthd()
    End Sub

    Private Sub btnfour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfour.Click

        mthd()
    End Sub

    Private Sub btnfive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfive.Click

        mthd()
    End Sub

    Private Sub btnsix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnsix.Click

        mthd()
    End Sub

    Private Sub btnseven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnseven.Click

        mthd()
    End Sub

    Private Sub btneight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btneight.Click

        mthd()
    End Sub

    Private Sub btnnine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnine.Click

        mthd()
    End Sub

    Public Sub mthd()

        If txtdisplay.Text = 0 Then
            txtdisplay.Text = Me.ActiveControl.Text
        Else
            txtdisplay.Text = txtdisplay.Text + Me.ActiveControl.Text
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        mthd()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        mthd()
    End Sub

    Private Sub btnplus_minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplus_minus.Click

        
        If txtdisplay.Text.StartsWith("-") Then
            txtdisplay.Text = txtdisplay.Text.Remove(0, 1)
        Else
            txtdisplay.Text = "-" + txtdisplay.Text
        End If
    End Sub

    Private Sub btnfraction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfraction.Click

        'calculating 1 divided by the value given
        txtdisplay.Text = (1 / txtdisplay.Text)
    End Sub

    Private Sub btnroot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnroot.Click

        'calculating the square root
        txtdisplay.Text = Math.Sqrt(txtdisplay.Text)
    End Sub

    Private Sub btnspace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnspace.Click

        ' Back_Space

        Dim lent As Integer

        lent = txtdisplay.TextLength

        If lent = 0 Then

        ElseIf lent = 1 Then
            txtdisplay.Text = 0
        Else
            txtdisplay.Text = txtdisplay.Text.Remove((lent) - 1)
        End If
    End Sub

    Private Sub btnplus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnplus.Click

        i = txtdisplay.Text
        sign = "+"
        txtdisplay.Text = 0
    End Sub

    Private Sub btnequals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnequals.Click

        If sign = "+" Then
            txtdisplay.Text = (i + txtdisplay.Text)

        ElseIf sign = "-" Then

            txtdisplay.Text = (i - txtdisplay.Text)

        ElseIf sign = "*" Then

            txtdisplay.Text = (i * txtdisplay.Text)

        ElseIf sign = "/" Then

            txtdisplay.Text = (i / txtdisplay.Text)
        End If
    End Sub

    Private Sub btnminus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnminus.Click

        i = txtdisplay.Text
        sign = "-"
        txtdisplay.Text = 0
    End Sub

    Private Sub btntimes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btntimes.Click

        i = txtdisplay.Text
        sign = "*"
        txtdisplay.Text = 0
    End Sub

    Private Sub btndivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndivide.Click

        i = txtdisplay.Text
        sign = "/"
        txtdisplay.Text = 0
    End Sub

    Private Sub btnmt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmt.Click

        If txtdisplay.Text = 0 Then

        Else

            memory = txtdisplay.Text
        End If
    End Sub

    Private Sub btnmr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnmr.Click

        txtdisplay.Text = memory
    End Sub

    Private Sub btnpercent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnpercent.Click

        txtdisplay.Text = (txtdisplay.Text / 100)
    End Sub

    

    Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        ' resize event
        Me.Width = 264
        Me.Height = 332
    End Sub

End Class


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Scientific Calculator
~~~~~~~~~~~~~~~~
....Scientific calculator is a type of electronic calculator designed to calculate problems in science, engineering and mathematics. They have almost completely replaced slide rules in almost all traditional applications, and are widely used in both education and professional settings. Definition from ~http://en.wikipedia.org/wiki/Scientific_calculator

....Simple scientific calculator design and source code using Microsoft visual basic.net 2010. for now, i could only add the listed/shown functions. 
....need the EXECUTABLE (.exe) file? no worries;
Calculating 4 combination 2 (4C2) AND logarithm of 6 (log6) respectively




















-----------
Source Code
-----------
Public Class frmScientific

    Dim memory As Double
    Public i As Double
    Public sign As String

    'nPr variables declaration
    Public start1 As Double
    Public start11 As Double
    Public count1 As Double = 1
    Public count11 As Double = 1
    Public ii As Double

    'nCr variables declaration
    Public start1c As Double
    Public sstart1c As Double
    Public count1c As Double = 1
    Public scount1c As Double = 1
    Public start1cc As Double
    Public count1cc As Double = 1
    Public iic As Double

    Public Sub mthd()
        If txtdisplay1.Text = 0 Then
            txtdisplay1.Text = Me.ActiveControl.Text
        Else
            txtdisplay1.Text = txtdisplay1.Text + Me.ActiveControl.Text
        End If

    End Sub

    Private Sub btn_del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_del.Click
        txtdisplay.Text = String.Empty
        ' BackSpace

        Dim txt_length As Integer
        txt_length = txtdisplay1.TextLength

        If txt_length = 0 Then
        ElseIf txt_length = 1 Then
            txtdisplay1.Text = 0
        Else
            txtdisplay1.Text = txtdisplay1.Text.Remove((txt_length) - 1)
        End If
    End Sub

    Private Sub btn_plusMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_plusMinus.Click

        ' Preceeding Addition/Subtraction
        If txtdisplay1.Text.StartsWith("-") Then
            txtdisplay1.Text = txtdisplay1.Text.Remove(0, 1)
        Else
            txtdisplay1.Text = "-" + txtdisplay1.Text
        End If
    End Sub

    Private Sub btn_zero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_zero.Click
        mthd()
    End Sub

    Private Sub btn_one_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_one.Click
        mthd()
    End Sub

    Private Sub btn_two_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_two.Click
        mthd()
    End Sub

    Private Sub btn_three_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_three.Click
        mthd()
    End Sub

    Private Sub btn_four_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_four.Click
        mthd()
    End Sub

    Private Sub btn_five_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_five.Click
        mthd()
    End Sub

    Private Sub btn_six_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_six.Click
        mthd()
    End Sub

    Private Sub btn_seven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_seven.Click
        mthd()
    End Sub

    Private Sub btn_eight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_eight.Click
        mthd()
    End Sub

    Private Sub btn_nine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nine.Click
        mthd()
    End Sub

    Private Sub btn_c_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_c.Click

        txtcover.Visible = False
        txtdisplay.Text = String.Empty
        txtdisplay1.Text = 0

        init()
    End Sub

    Private Sub btn_dot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_dot.Click
        mthd()
    End Sub

    Private Sub btn_multiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_multiply.Click
        txtdisplay.Text = String.Empty

        i = txtdisplay1.Text
        sign = "*"
        txtdisplay1.Text = 0
    End Sub

    Private Sub btn_divide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_divide.Click
        txtdisplay.Text = String.Empty

        i = txtdisplay1.Text
        sign = "/"
        txtdisplay1.Text = 0
    End Sub

    Private Sub btn_plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_plus.Click
        txtdisplay.Text = String.Empty

        i = txtdisplay1.Text
        sign = "+"
        txtdisplay1.Text = 0
    End Sub

    Private Sub btn_minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_minus.Click
        txtdisplay.Text = String.Empty

        i = txtdisplay1.Text
        sign = "-"
        txtdisplay1.Text = 0
    End Sub

    Private Sub btn_equals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_equals.Click
        txtdisplay.Text = String.Empty
        '  Dim all As String

        If sign = "+" Then
            txtdisplay.Text = i.ToString + " " + "+" + " " + txtdisplay1.Text.ToString
            txtdisplay1.Text = (i + txtdisplay1.Text)

        ElseIf sign = "-" Then
            txtdisplay.Text = i.ToString + " " + "-" + " " + txtdisplay1.Text.ToString
            txtdisplay1.Text = (i - txtdisplay1.Text)

        ElseIf sign = "*" Then
            txtdisplay.Text = i.ToString + " " + "*" + " " + txtdisplay1.Text.ToString
            txtdisplay1.Text = (i * txtdisplay1.Text)

        ElseIf sign = "/" Then
            txtdisplay.Text = i.ToString + " " + "/" + " " + txtdisplay1.Text.ToString
            txtdisplay1.Text = (i / txtdisplay1.Text)

        ElseIf sign = "p" Then

            Dim v As Double
            v = (ii - txtdisplay1.Text)

            For Me.start11 = 1 To v
                count11 = count11 * start11
            Next start11

            txtdisplay.Text = ii.ToString + "P" + txtdisplay1.Text.ToString
            txtdisplay1.Text = (count1 / count11)

            init()

        ElseIf sign = "c" Then
            '''''''''
            Dim vvv As Double
            vvv = (iic - txtdisplay1.Text)

            For Me.sstart1c = 1 To vvv
                scount1c = scount1c * sstart1c
            Next sstart1c
            ''''''''''
            Dim vv As Double
            vv = txtdisplay1.Text

            For Me.start1cc = 1 To vv
                count1cc = count1cc * start1cc
            Next start1cc
            '''''''''
            txtdisplay.Text = iic.ToString + "C" + txtdisplay1.Text.ToString
            txtdisplay1.Text = (count1c / (scount1c * count1cc))

            init()

        ElseIf sign = "^" Then

            txtdisplay.Text = i.ToString + "^" + txtdisplay1.Text.ToString
            txtdisplay1.Text = Math.Pow(i, txtdisplay1.Text)
        End If

    End Sub

    Private Sub btn_nCr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nCr.Click

        iic = txtdisplay1.Text
        i = txtdisplay1.Text
        sign = "c"

        For Me.start1c = 1 To i
            count1c = count1c * start1c
        Next start1c

        txtdisplay1.Text = 0
       
    End Sub

    Private Sub btn_factorial_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_factorial.Click

        txtdisplay.Text = txtdisplay1.Text.ToString + "!"

        Dim start As Double
        Dim count As Double = 1

        For start = 1 To txtdisplay1.Text
            count = count * start
        Next start

        txtdisplay1.Text = count

    End Sub

    Private Sub btn_off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_off.Click

        txtcover.Visible = True
        txtdisplay1.Text = 0
        txtdisplay.Text = String.Empty

    End Sub

    Private Sub btn_nPr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_nPr.Click

        ii = txtdisplay1.Text
        i = txtdisplay1.Text
        sign = "p"

        For Me.start1 = 1 To i
            count1 = count1 * start1
        Next start1

        txtdisplay1.Text = 0

    End Sub

    Private Sub btn_x3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_x3.Click

        txtdisplay.Text = txtdisplay1.Text.ToString + "^" + "3"
        txtdisplay1.Text = Math.Pow(txtdisplay1.Text, 3)

    End Sub

    Private Sub btn_log_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_log.Click

        txtdisplay.Text = "log" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = Math.Log(txtdisplay1.Text)

    End Sub

    Private Sub btn_sin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sin.Click

        txtdisplay.Text = "Sin" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = Math.Sin(txtdisplay1.Text)
    End Sub

    Private Sub btn_cos_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cos.Click

        txtdisplay.Text = "Cos" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = Math.Cos(txtdisplay1.Text)

    End Sub

    Private Sub btn_tan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_tan.Click

        txtdisplay.Text = "Tan" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = Math.Tan(txtdisplay1.Text)

    End Sub

    Private Sub btn_pi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_pi.Click

        txtdisplay1.Text = Math.PI

    End Sub

    Private Sub btn_squareroot_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_squareroot.Click

        txtdisplay.Text = "Sqrt" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = Math.Sqrt(txtdisplay1.Text)
    End Sub

    Private Sub btn_percent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_percent.Click

        txtdisplay.Text =  txtdisplay1.Text.ToString + "%"
        txtdisplay1.Text = (txtdisplay1.Text / 100)
    End Sub

    Private Sub btn_fraction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_fraction.Click

        txtdisplay.Text = "1/" + txtdisplay1.Text.ToString
        txtdisplay1.Text = (1 / txtdisplay1.Text)

    End Sub

    Private Sub btn_x_y_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_x_y.Click

        i = txtdisplay1.Text
        sign = "^"
        txtdisplay1.Text = 0
    End Sub

    Private Sub btn_x2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_x2.Click

        txtdisplay.Text = txtdisplay1.Text.ToString + "^" + "2"
        txtdisplay1.Text = Math.Pow(txtdisplay1.Text, 2)

    End Sub

    Private Sub btn_exp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_exp.Click

        txtdisplay.Text = "Exp" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = Math.Exp(txtdisplay1.Text)

    End Sub

    Private Sub btn_logInverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_logInverse.Click

        txtdisplay.Text = "log^-1" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = (1 / Math.Log(txtdisplay1.Text))

    End Sub

    Private Sub btn_sinInverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_sinInverse.Click

        txtdisplay.Text = "Sin^-1" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = (1 / Math.Sin(txtdisplay1.Text))

    End Sub

    Private Sub btn_cosInverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_cosInverse.Click

        txtdisplay.Text = "Cos^-1" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = (1 / Math.Cos(txtdisplay1.Text))

    End Sub

    Private Sub btn_tanInverse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_tanInverse.Click

        txtdisplay.Text = "Tan^-1" + " " + txtdisplay1.Text.ToString
        txtdisplay1.Text = (1 / Math.Tan(txtdisplay1.Text))

    End Sub

    Private Sub btn_mt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_mt.Click

        If txtdisplay1.Text = 0 Then

        Else
            memory = txtdisplay1.Text
        End If

    End Sub

    Private Sub btn_mr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_mr.Click

        txtdisplay.Text = "m"
        txtdisplay1.Text = memory
    End Sub


    Public Sub init()
        i = 0
        start1 = 0
        start11 = 0
        count1 = 1
        count11 = 1
        ii = 0
        start1c = 0
        sstart1c = 0
        count1c = 1
        scount1c = 1
        start1cc = 0
        count1cc = 1
        iic = 0
    End Sub

    Private Sub frmScientific_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        Me.Width = 266
        Me.Height = 451
    End Sub
End Class