Ask Experts Questions for FREE Help !
Ask

Visual basic code for calculator

Asked Feb 12, 2009, 12:11 AM — 8 Answers
I am a student at college in somaliland. One of my instructors told me to write a small programme for a calculator in visual basic 6.0. So, I'm stuck with this. Help me with the codes.

8 Answers
rwinterton's Avatar
rwinterton Posts: 289, Reputation: 78
Full Member
 
#2

Feb 14, 2009, 06:13 AM
Start by putting five buttons on a form and a two textboxes to hold numbers. Put "+", "-", "*", "/" and "C" on each of the four buttons (or "Add", "Sub", "Mul", "Div" and "Clear" respectively.

Make one textbox "read/only". And put a "0" in the box. That will be the "answer box". Put a "0" in the other box. That will be the "user's box". Name the boxes "txtAnswer" and "txtOperand"

Double-click on each key to create a key-click handler.

The user will enter a value in his text box and click one of the keys. When he clicks the key, you either add, subtract, multiply, or divide (depending on the key that is clicked) the value in the "answer" box with the number in the user's box. Place the result in the answer box.

Here's code for the "+" handler:

Dim NewValue as double = cdbl(txtOperand.text)
Dim OldValue as double = cdbl(txtAnswer.text)
Dim Answer as double
' if the "ADD" button is clicked, add them.
Answer = NewValue + OldValue
txtAnswer.text = cstr(Answer)

Do this for each of the buttons.

When the user hits the "Clear" button, put a zero in the "answer" box.
Helpful
nano12's Avatar
nano12 Posts: 1, Reputation: 1
New Member
 
#3

Mar 29, 2009, 03:58 AM
I am a physics student and my professor asked me to write a visual basic 2005 for both standard and scientific calculator and I have no idea what to do please help me
Helpful
ashish246's Avatar
ashish246 Posts: 2, Reputation: 10
Junior Member
 
#4

Jul 3, 2011, 11:33 PM
Write whatever you know you will get a super scientific calculator..
Helpful
ashish246's Avatar
ashish246 Posts: 2, Reputation: 10
Junior Member
 
#5

Jul 3, 2011, 11:36 PM
If you **** you will get better super scientific calculator..
Helpful
jono2212's Avatar
jono2212 Posts: 1, Reputation: 10
Junior Member
 
#6

Jan 15, 2012, 02:10 AM
Codes that you are looking for:
MC:
Mem = 0
TextBox1.Text = "0"
MR:
TextBox1.Text = Mem
C:
TextBox1.Text = "0"
Value1 = 0
Value2 = 0
Oper = ""
M+:
Mem = Mem + Val(TextBox1.Text)
M-:
Mem = Mem - Val(TextBox1.Text)
<-:
TextBox1.Text = Val(TextBox1.Text) \ 10
CE:
Value2 = "0"
Oper = ""
TextBox1.Text = "0"
√:
TextBox1.Text = Math.Sqrt(Val(TextBox1.Text))
±:
TextBox1.Text = Val(TextBox1.Text) * -1
1/x:
TextBox1.Text = 1 / Val(TextBox1.Text)
%:
TextBox1.Text = Value1 * (Val(TextBox1.Text) / 100)
Helpful
walalolo's Avatar
walalolo Posts: 1, Reputation: 1
New Member
 
#7

Mar 31, 2012, 11:04 AM
I want to mod ????
Helpful
younGrassHopper's Avatar
younGrassHopper Posts: 1, Reputation: 1
New Member
 
#8

Jun 6, 2012, 12:13 AM
Hey, I tried using the following code before coming to this site, and when I debug all it does is give me a value of 0 in Textbox1 , sort of confused here

Code Used :
TextBox1.Text = Value1 * (Val(TextBox1.Text) / 100)

Any help is much appreciated
Helpful
LEDOR's Avatar
LEDOR Posts: 1, Reputation: 1
New Member
 
#9

Jul 19, 2012, 04:42 AM
Option Explicit

Private mdblResult As Double
Private mdblSavedNumber As Double
Private mstrDot As String
Private mstrOp As String
Private mstrDisplay As String
Private mblnDecEntered As Boolean
Private mblnOpPending As Boolean
Private mblnNewEquals As Boolean
Private mblnEqualsPressed As Boolean
Private mintCurrKeyIndex As Integer

Private Sub Form_Load()

Top = (Screen.Height - Height) / 2
Left = (Screen.Width - Width) / 2

End Sub

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

Dim intIndex As Integer

Select Case KeyCode
Case vbKeyBack: intIndex = 0
Case vbKeyDelete: intIndex = 1
Case vbKeyEscape: intIndex = 2
Case vbKey0, vbKeyNumpad0: intIndex = 18
Case vbKey1, vbKeyNumpad1: intIndex = 13
Case vbKey2, vbKeyNumpad2: intIndex = 14
Case vbKey3, vbKeyNumpad3: intIndex = 15
Case vbKey4, vbKeyNumpad4: intIndex = 8
Case vbKey5, vbKeyNumpad5: intIndex = 9
Case vbKey6, vbKeyNumpad6: intIndex = 10
Case vbKey7, vbKeyNumpad7: intIndex = 3
Case vbKey8, vbKeyNumpad8: intIndex = 4
Case vbKey9, vbKeyNumpad9: intIndex = 5
Case vbKeyDecimal: intIndex = 20
Case vbKeyAdd: intIndex = 21
Case vbKeySubtract: intIndex = 16
Case vbKeyMultiply: intIndex = 11
Case vbKeyDivide: intIndex = 6
Case Else: Exit Sub
End Select

cmdCalc(intIndex).SetFocus
cmdCalc_Click intIndex

End Sub

Private Sub Form_KeyPress(KeyAscii As Integer)

Dim intIndex As Integer

Select Case Chr$(KeyAscii)
Case "S", "s": intIndex = 7
Case "P", "p": intIndex = 12
Case "R", "r": intIndex = 17
Case "X", "x": intIndex = 11
Case "=": intIndex = 22
Case Else: Exit Sub
End Select

cmdCalc(intIndex).SetFocus
cmdCalc_Click intIndex

End Sub

Private Sub cmdCalc_Click(Index As Integer)

Dim strPressedKey As String

mintCurrKeyIndex = Index

If mstrDisplay = "ERROR" Then
mstrDisplay = ""
End If

strPressedKey = cmdCalc(Index).Caption

Select Case strPressedKey
Case "0", "1", "2", "3", "4", _
"5", "6", "7", "8", "9"
If mblnOpPending Then
mstrDisplay = ""
mblnOpPending = False
End If
If mblnEqualsPressed Then
mstrDisplay = ""
mblnEqualsPressed = False
End If
mstrDisplay = mstrDisplay & strPressedKey
Case "."
If mblnOpPending Then
mstrDisplay = ""
mblnOpPending = False
End If
If mblnEqualsPressed Then
mstrDisplay = ""
mblnEqualsPressed = False
End If
If InStr(mstrDisplay, ".") > 0 Then
Beep
Else
mstrDisplay = mstrDisplay & strPressedKey
End If
Case "+", "-", "X", "/"
mdblResult = Val(mstrDisplay)
mstrOp = strPressedKey
mblnOpPending = True
mblnDecEntered = False
mblnNewEquals = True
Case "%"
mdblSavedNumber = (Val(mstrDisplay) / 100) * mdblResult
mstrDisplay = Format$(mdblSavedNumber)
Case "="
If mblnNewEquals Then
mdblSavedNumber = Val(mstrDisplay)
mblnNewEquals = False
End If
Select Case mstrOp
Case "+"
mdblResult = mdblResult + mdblSavedNumber
Case "-"
mdblResult = mdblResult - mdblSavedNumber
Case "X"
mdblResult = mdblResult * mdblSavedNumber
Case "/"
If mdblSavedNumber = 0 Then
mstrDisplay = "ERROR"
Else
mdblResult = mdblResult / mdblSavedNumber
End If
Case Else
mdblResult = Val(mstrDisplay)
End Select
If mstrDisplay <> "ERROR" Then
mstrDisplay = Format$(mdblResult)
End If
mblnEqualsPressed = True
Case "+/-"
If mstrDisplay <> "" Then
If Left$(mstrDisplay, 1) = "-" Then
mstrDisplay = Right$(mstrDisplay, 2)
Else
mstrDisplay = "-" & mstrDisplay
End If
End If
Case "Backspace"
If Val(mstrDisplay) <> 0 Then
mstrDisplay = Left$(mstrDisplay, Len(mstrDisplay) - 1)
mdblResult = Val(mstrDisplay)
End If
Case "CE"
mstrDisplay = ""
Case "C"
mstrDisplay = ""
mdblResult = 0
mdblSavedNumber = 0
Case "1/x"
If Val(mstrDisplay) = 0 Then
mstrDisplay = "ERROR"
Else
mdblResult = Val(mstrDisplay)
mdblResult = 1 / mdblResult
mstrDisplay = Format$(mdblResult)
End If
Case "sqrt"
If Val(mstrDisplay) < 0 Then
mstrDisplay = "ERROR"
Else
mdblResult = Val(mstrDisplay)
mdblResult = Sqr(mdblResult)
mstrDisplay = Format$(mdblResult)
End If
End Select

If mstrDisplay = "" Then
lblDisplay = "0."
Else
mstrDot = IIf(InStr(mstrDisplay, ".") > 0, "", ".")
lblDisplay = mstrDisplay & mstrDot
If Left$(lblDisplay, 1) = "0" Then
lblDisplay = Mid$(lblDisplay, 2)
End If
End If

If lblDisplay = "." Then lblDisplay = "0."

End Sub
Helpful

Not your question? Ask your question View similar questions

 
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Add your answer here.

Remove Text Formatting

Undo
Redo
 
Decrease Size
Increase Size
Bold
Italic
Underline
Align Left
Align Center
Align Right
Ordered List
Unordered List
Decrease Indent
Increase Indent
Insert Email Link
Wrap [QUOTE] tags around selected text
Wrap [CODE] tags around selected text
Wrap [HTML] tags around selected text
Wrap [PHP] tags around selected text
Wrap [YOUTUBE] tags around selected text
Notification Type:



Check out some similar questions!

Visual Basic [ 1 Answers ]

Actually I'm a new user(student) of Visual Basic.So I want to know how I am make a simple calculater in Visual Basic 6?

How to make a calculator in visual basic 6 [ 1 Answers ]

How to make a calculator in visual basic 6, what are the codes for it?

Visual Basic Code [ 3 Answers ]

Hi There, I have a VBA macro from work which transfers our reports from one excel sheet to another and then updates itself to particular dates under each user name. I am totally confused how it works... Because it doesn't use any kind of link with the file. I have done VB 6.0 and am out of...


View more Visual Basic questions Search