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.
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.
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
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