WELLCOME

Visual Basic is a very easy and powerful programming language, a very simple coding of the software can be created with Visual Basic. I have my Blogger (http://www.vbcodebd.blogspot.com) in Visual basic 6.0 via kinds Program will discuss the code. Thanks All ..........

Monday, January 28, 2013

Full Calculator Project (Visual Basic Tutorial - 7)



Create a form with      1 text boxe , 1 Label , And 24 command buttons.

Text box properties…………..
Named: txtDisplay
Alignment: 1-Right Justify
Enabled: False
Text: 0

Command buttons properties…………..
Command buttons 1 - Name: cmdAllClear
                                    Capuion: AC
Command buttons 2 - Name: cmdClear
                                    Capuion: C
Command buttons 3 - Name: cmdSquareRoot
                                    Capuion: Sqrt
Command buttons 4 - Name: cmdPercent
                                    Capuion: %
Command buttons 5 - Name: cmdOperatort
                                    Capuion: /
                                    Index: 0

Command buttons 6 - Name: cmdMemoryClear
                                    Capuion: MC
Command buttons 7 - Name: cmdNumber
                                    Capuion: 7
                                    Index: 6
Command buttons 8 - Name: cmdNumber
                                    Capuion: 8
                                    Index: 7
Command buttons 9 - Name: cmdNumbert
                                    Capuion: 9
                                    Index: 8
Command buttons 10 - Name: cmdOperato
                                    Capuion: *
                                    Index: 1

Command buttons 11 - Name: cmdMemoryReminder
                                    Capuion: MR
Command buttons 12 - Name: cmdNumber
                                    Capuion: 4
Index: 3
Command buttons 13 - Name: cmdNumber
                                    Capuion: 5
                                    Index: 4
Command buttons 14 - Name: cmdNumber
                                    Capuion: 6
                                    Index: 5
Command buttons 15 - Name: cmdOperator
                                    Capuion: -
                                    Index: 2

Command buttons 16 - Name: cmdMemoryPlus
                                    Capuion: M+
Command buttons 17 - Name: cmdNumber
                                    Capuion: 1
                                    Index: 0
Command buttons 18 - Name: cmdNumbert
                                    Capuion: 2
                                    Index: 1
Command buttons 19 - Name: cmdNumber
                                    Capuion: 3
                                    Index: 2
Command buttons 20 - Name: cmdOperator
                                    Capuion: +
                                    Index: 3

Command buttons 21 - Name: cmdMemoryMinus
                                    Capuion: M-
Command buttons 22 - Name: cmdNumber
                                    Capuion: 0
                                    Index: 9
Command buttons 23 - Name: cmdPoint
                                    Capuion: .
Command buttons 24 - Name: cmdEqual
                                    Capuion: =.

Add the following code to the form code module for the ‘frmCalculator’ _Click event:

Program code:

Dim FirstNumber As Double
Dim LastNumber As Double
Dim Operator As String
Dim ClearDisplay As Boolean
Dim Result As Double
Dim Memory As Double

Private Sub cmdAllClear_Click()
txtDisplay.Text = "0"
Operator = ""
FirstNumber = 0
LastNumber = 0
ClearDisplay = False
Result = 0
End Sub

Private Sub cmdClear_Click()
txtDisplay.Text = "0"
End Sub

Private Sub cmdEqual_Click()
'sets the value of the variable "LastNumber"
LastNumber = Val(txtDisplay.Text)
'it exits if no operator was pressed before
If Operator = "" Then Exit Sub
'checks which operator was pressed
'then calculates the result accordingly to that operator
If Operator = "+" Then Result = FirstNumber + LastNumber
If Operator = "-" Then Result = FirstNumber - LastNumber
If Operator = "*" Then Result = FirstNumber * LastNumber
If Operator = "/" Then
    'shows an error messege if the division number=0
    'otherwise calculates the result
    If LastNumber = 0 Then
        txtDisplay.Text = "Error: Positive Infinity"
        ClearDisplay = True
        Exit Sub
    Else
        Result = FirstNumber / LastNumber
    End If
End If
'shows the result in the Display Box
txtDisplay.Text = Str(Result)
ClearDisplay = True
Operator = ""
End Sub

Private Sub cmdMemoryClear_Click()
Memory = 0
lblMemory.Caption = ""
ClearDisplay = True
End Sub

Private Sub cmdMemoryMinus_Click()
Memory = Memory - Val(txtDisplay.Text)
lblMemory.Caption = "M"
ClearDisplay = True
End Sub

Private Sub cmdMemoryPlus_Click()
Memory = Memory + Val(txtDisplay.Text)
lblMemory.Caption = "M"
ClearDisplay = True
End Sub


Private Sub cmdMemoryReminder_Click()
txtDisplay.Text = Str(Memory)
ClearDisplay = True
End Sub

Private Sub cmdNumber_Click(Index As Integer)
'if any operator is pressed or newly starts calculating,
'the Display box will be cleared
If ClearDisplay = True Then txtDisplay.Text = ""
'clears the Display box if it shows only "0" because
'"0" cannot be the first digit of a whole number
If txtDisplay.Text = "0" Then txtDisplay.Text = ""
'writes the number that is shown on the button
txtDisplay.Text = txtDisplay.Text & cmdNumber(Index).Caption
'sets the value "False" or the variable ClearDisplay
ClearDisplay = False
End Sub

Private Sub cmdOperator_Click(Index As Integer)
'sets the value of the variable "FirstNumber"
FirstNumber = Val(txtDisplay.Text)
'sets which operator is pressed
Operator = cmdOperator(Index).Caption
'sets cleardisplay to "False"
ClearDisplay = True
End Sub

Private Sub cmdPoint_Click()
'if the point button is pressed after pressing any operator
'it starts a new decimal number
If ClearDisplay = True Then
    txtDisplay.Text = "0."
    ClearDisplay = False
End If
'checks if there is any point in the number in the txtDisplay
'if no point found it sets a new point
If InStr(txtDisplay.Text, ".") = 0 Then
    txtDisplay.Text = txtDisplay.Text & "."
    ClearDisplay = False
End If
End Sub

Private Sub cmdSquareRoot_Click()
'shows an error messege if it is a negative number
'otherwise calculates the squareroot value of the number
If Val(txtDisplay.Text) >= 0 Then
    txtDisplay.Text = Sqr(Val(txtDisplay.Text))
Else
    txtDisplay.Text = "Invalid Input for Function"
End If
ClearDisplay = True
End Sub

Private Sub Form_Load()
Top = (Screen.Height - Height) / 2
Left = (Screen.Width - Widht) / 3

End Sub


………………………………………….........................

Download this Calculator Project File  click hare 14 kb

………………………………………….........................

4 comments:

  1. solve Multiple expressions in one line . Here is a calculator for that . Download with source code from here :-
    http://geeksprogrammings.blogspot.in/2013/09/make-calculator-in-net.html

    ReplyDelete
  2. That's a very complex coding...
    I can do it in an easier and better process

    ReplyDelete
  3. Need code for MR MC and M+ in VB

    ReplyDelete
  4. Tutorial Vb: Full Calculator Project (Visual Basic Tutorial - 7) >>>>> Download Now

    >>>>> Download Full

    Tutorial Vb: Full Calculator Project (Visual Basic Tutorial - 7) >>>>> Download LINK

    >>>>> Download Now

    Tutorial Vb: Full Calculator Project (Visual Basic Tutorial - 7) >>>>> Download Full

    >>>>> Download LINK Pt

    ReplyDelete