Results 1 to 4 of 4

Thread: Calculator program using visual basic

  1. #1
    Join Date
    Nov 2009
    Posts
    154

    Calculator program using visual basic

    I got an assignment from college and I need to create calculating application using visual basic. I am very poor in a programming so some one help me to write program in visual basic. I can create user interface only but can not write code for each button so help me write program.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Calculator program using visual basic

    Code:
    Private Operator  As String
    Private First As String
    
    
    Private Sub cmdClear_Click()
        First = ""
        Operator = ""
    End Sub
    
    Private Sub cmdEquals_Click()
    Select Case Operator
        Case Is = "+"
            Text1.Text = First + Text1.Text
        Case Is = "-"
            Text1.Text = First - Text1.Text
        Case Is = "/"
            Text1.Text = First / Text1.Text
        Case Is = "*"
            Text1.Text = First * Text1.Text
    End Select
        First = ""
        Operator = ""
    End Sub
    
    Private Sub cmdOperator_Click(Index As Integer)
        Operator = cmdOperator(Index).Caption
        First = Text1.Text
        Text1.Text = ""
    End Sub
    
    Private Sub Command1_Click(Index As Integer)
        Text1.Text = Text1.Text + Command1(Index).Caption
    End Sub

    This program will help you.
    Last edited by Praetor; 24-11-2009 at 07:11 PM.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: Calculator program using visual basic

    Code:
    Private LastOpe  As String
    Private num As String
    Private Sub Form_Load()
        txtResult.Enabled = False
        Me.KeyPreview = True
    End Sub
    
    Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
        ' numbers in main keyboard
        If (KeyCode >= 48 And KeyCode <= 57) Then
            AddDigits (Chr(KeyCode))
        End If
       
        ' numbers in numpad
        If (KeyCode >= 96 And KeyCode <= 105) Then
            AddDigits (Chr(KeyCode - 48))
        End If
       
        'if backspace
        If (KeyCode = 8) Then
            If (txtResult.Text <> "") Then
            txtResult.Text = Left(txtResult.Text, Len(txtResult.Text) - 1)
            End If
        End If
        'for various operators
        If (KeyCode = 111) Then RegisterOperator ("/")
        If (KeyCode = 106) Then RegisterOperator ("*")
        If (KeyCode = 109) Then RegisterOperator ("-")
        If (KeyCode = 107) Then RegisterOperator ("+")
    End Sub
    Private Sub cmdOperator_Click(Index As Integer)
        RegisterOperator (cmdOperator(Index).Caption)
    End Sub
    Private Sub cmdClear_Click()
       num = ""
        LastOpe = ""
        txtResult.Text = ""
       
    End Sub
    Private Sub cmdEquals_Click()
        Select Case LastOpe
            Case Is = "+"
                txtResult.Text = num+ txtResult.Text
            Case Is = "-"
                txtResult.Text = num - txtResult.Text
            Case Is = "/"
                txtResult.Text = num / txtResult.Text
            Case Is = "*"
                txtResult.Text =num * txtResult.Text
        End Select
            num = ""
            LastOpe = ""
    End Sub
    Private Sub Command1_Click(Index As Integer)
        AddDigits (Command1(Index).Caption)
    End Sub
     
    Private Sub AddDigits(Digit As String)
        txtResult.Text = txtResult.Text & Digit
    End Sub
    Private Sub RegisterOperator(OperatorText As String)
        LastOpe = OperatorText
        num = txtResult.Text
        txtResult.Text = ""
    End Sub

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Calculator program using visual basic

    Code:
       Dim first As Double
       Dim second As Double
       Dim opr As String 
    
    
    Private Sub Command1_Click(Index As Integer)
            If txtView.Text = "" Then
              txtView.Text = Command1(Index).Caption
            Else
                 txtView.Text = txtView.Text & Command1(Index).Caption
            End If
       End Sub 
    
    
    Private Sub Command2_Click(Index As Integer)
            first = txtView.Text
            txtView.Text = ""
            opr= Command2(Index).Caption
       End Sub 
    
    Private Sub Form_Load()
       txtView.Enabled = False
       txtView.MaxLength = 10
       End Sub 
    
    Private Sub cmdEq_Click()
            second = txtView.Text
           If opr= "-" Then
                 txtView.Text = first - second
            ElseIf opr = "+" Then
                 txtView.Text = first + second
            ElseIf opr = "*" Then
                 txtView.Text  = first * second
            ElseIf opr = "/" Then
                 txtView.Text = first / second
            End If
       End Sub 
    
    
    Private Sub cmdDot_Click()
            If InStr(txtView.Text, ".") = 0 Then
                 txtView.Text = txtView.Text & "."
            End If
       End Sub 
    
    Private Sub cmdClear_Click()
           txtView.Text = "0"
       End Sub

Similar Threads

  1. How to eliminate the bugs in a program of visual basic?
    By sRIPRIYA in forum Software Development
    Replies: 4
    Last Post: 03-01-2011, 10:46 PM
  2. What are the steps to execute Visual basic program?
    By Asis in forum Software Development
    Replies: 3
    Last Post: 02-01-2011, 08:22 AM
  3. Help me program a calculator in java
    By Kelvin Little in forum Software Development
    Replies: 4
    Last Post: 28-06-2010, 05:55 PM
  4. Is GUI same like Visual Basic ?
    By Caesar in forum Software Development
    Replies: 2
    Last Post: 02-03-2009, 01:32 PM
  5. Visual Basic 2005 or Visual Basic 6
    By Aasha in forum Software Development
    Replies: 5
    Last Post: 15-01-2009, 06:56 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,646,541.29441 seconds with 17 queries