Tuesday, June 28, 2016

MINI CALCULATOR PROGRAM IN VB.NET

Source code
Public Class frmCalculator
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnresult_Click(sender As Object, e As EventArgs) Handles btnresult.Click
        If frmvaildate() Then
            Dim fnum, snum, result As Double
            Dim opt As String

            fnum = Double.Parse(txtfnumber.Text)
            snum = Double.Parse(txtsnum.Text)
            opt = txtopt.Text

            Select Case (opt)
                Case "+"
                    result = fnum + snum
                Case "-"
                    result = fnum - snum
                Case "*"
                    result = fnum * snum
                Case "/"
                    If snum = 0 Then
                        MessageBox.Show("No is not  div by zero")
                        Return
                    Else
                        result = fnum / snum
                    End If
                Case Else
                    MessageBox.Show("Invalid Operation")
                    txtopt.Clear()
                    txtopt.Focus()
                    Return
            End Select
            txtresult.Text = result.ToString()
        End If

    End Sub
    Public Function frmvaildate() As Boolean
        Dim flag As Boolean = True
        If (txtfnumber.Text.Length = 0) Then
            MessageBox.Show("Enter First Number")
            txtfnumber.Focus()
            flag = False
        ElseIf Not (IsNumeric(txtfnumber.Text))
            MessageBox.Show("Enter only numeric value")
            txtfnumber.Clear()
            txtfnumber.Focus()
            flag = False
        ElseIf txtsnum.Text.Length = 0
            MessageBox.Show("Enter Second Number")
            txtsnum.Focus()
            flag = False
        ElseIf Not (IsNumeric(txtsnum.Text))
            MessageBox.Show("Enter only numeric value")
            txtsnum.Clear()
            txtsnum.Focus()
            flag = False
        ElseIf txtopt.Text.Length = 0
            MessageBox.Show("Enter Operator")
            txtopt.Focus()
            flag = False
        End If
        Return flag
    End Function
    Private Sub txtfnumber_KeyDown(sender As Object, e As KeyEventArgs) Handles txtfnumber.KeyDown
        If (Integer.Parse(e.KeyCode()) = 13) Then
            txtsnum.Focus()
        End If
    End Sub
    Private Sub txtsnum_KeyDown(sender As Object, e As KeyEventArgs) Handles txtsnum.KeyDown
        If (Integer.Parse(e.KeyCode()) = 13) Then
            txtopt.Focus()
        End If
    End Sub
    Private Sub txtopt_KeyDown(sender As Object, e As KeyEventArgs) Handles txtopt.KeyDown
        If (Integer.Parse(e.KeyCode()) = 13) Then
            btnresult.Focus()
        End If
    End Sub

    Private Sub btnclear_Click(sender As Object, e As EventArgs) Handles btnclear.Click
        clear()
    End Sub
    Public Sub clear()
        txtfnumber.Clear()
        txtsnum.Clear()
        txtopt.Clear()
        txtresult.Clear()
        txtfnumber.Focus()
    End Sub
End Class


5 comments: