Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 24-11-2009
Member
 
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.
Reply With Quote
  #2  
Old 24-11-2009
Member
 
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.
Reply With Quote
  #3  
Old 24-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
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
Reply With Quote
  #4  
Old 24-11-2009
Member
 
Join Date: May 2008
Posts: 2,293
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 08:09 PM.