|
|
|
|
New Member
|
|
Feb 12, 2009, 01:11 AM
|
|
visual basic code for calculator
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.
|
|
|
Full Member
|
|
Feb 14, 2009, 07: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.
|
|
|
New Member
|
|
Mar 29, 2009, 03:58 AM
|
|
Iam 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
|
|
|
New Member
|
|
Jul 3, 2011, 11:33 PM
|
|
Write whatever u know u will get a super scientific calculator..
|
|
|
New Member
|
|
Jul 3, 2011, 11:36 PM
|
|
If u **** u will get better super scientific calculator..
|
|
|
New Member
|
|
Jan 15, 2012, 03: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 = "
M+:
Mem = Mem + Val(TextBox1.Text)
M-:
Mem = Mem - Val(TextBox1.Text)
<-:
TextBox1.Text = Val(TextBox1.Text) \ 10
CE:
Value2 = "
Oper = ""
TextBox1.Text = "
Oper = "
√:
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)
|
|
|
New Member
|
|
Mar 31, 2012, 11:04 AM
|
|
I want to mod??
|
|
|
New Member
|
|
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
|
|
|
New Member
|
|
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 "+", "-", ", ", "/", "%"
mdblSavedNumber = (Val(mstrDisplay) / 100) * mdblResult
mstrDisplay = Format$(mdblSavedNumber)
Case "
mdblResult = Val(mstrDisplay)
mstrOp = strPressedKey
mblnOpPending = True
mblnDecEntered = False
mblnNewEquals = True
Case "
If mblnNewEquals Then
mdblSavedNumber = Val(mstrDisplay)
mblnNewEquals = False
End If
Select Case mstrOp
Case "+"
mdblSavedNumber = (Val(mstrDisplay) / 100) * mdblResult
mstrDisplay = Format$(mdblSavedNumber)
Case "-"
mdblResult = mdblResult - mdblSavedNumber
Case "
If mblnNewEquals Then
mdblSavedNumber = Val(mstrDisplay)
mblnNewEquals = False
End If
Select Case mstrOp
Case "
mdblResult = mdblResult * mdblSavedNumber
Case "/"
If mdblSavedNumber = 0 Then
mstrDisplay = "
mdblResult = mdblResult + mdblSavedNumber
Case "
Else
mdblResult = mdblResult / mdblSavedNumber
End If
Case Else
mdblResult = Val(mstrDisplay)
End Select
If mstrDisplay <> "
mdblResult = mdblResult - mdblSavedNumber
Case " Then
mstrDisplay = Format$(mdblResult)
End If
mblnEqualsPressed = True
Case "+/-"
mdblResult = mdblResult * mdblSavedNumber
Case ""
If mdblSavedNumber = 0 Then
mstrDisplay = "-"
Else
mdblResult = mdblResult / mdblSavedNumber
End If
Case Else
mdblResult = Val(mstrDisplay)
End Select
If mstrDisplay <> "-" Then
mstrDisplay = Format$(mdblResult)
End If
mblnEqualsPressed = True
Case "Backspace"
If mstrDisplay <> "CE" Then
If Left$(mstrDisplay, 1) = "" Then
mstrDisplay = Right$(mstrDisplay, 2)
Else
mstrDisplay = "C" & mstrDisplay
End If
End If
Case ""
If Val(mstrDisplay) <> 0 Then
mstrDisplay = Left$(mstrDisplay, Len(mstrDisplay) - 1)
mdblResult = Val(mstrDisplay)
End If
Case "1/x"
If Val(mstrDisplay) = 0 Then
mstrDisplay = "
mstrDisplay = "
Else
mdblResult = Val(mstrDisplay)
mdblResult = 1 / mdblResult
mstrDisplay = Format$(mdblResult)
End If
Case "sqrt"
If Val(mstrDisplay) < 0 Then
mstrDisplay = "
Case "
Else
mdblResult = Val(mstrDisplay)
mdblResult = Sqr(mdblResult)
mstrDisplay = Format$(mdblResult)
End If
End Select
If mstrDisplay = ""
mstrDisplay = "0."
mdblResult = 0
mdblSavedNumber = 0
Case "."
If Val(mstrDisplay) = 0 Then
mstrDisplay = ""
Else
mdblResult = Val(mstrDisplay)
mdblResult = 1 / mdblResult
mstrDisplay = Format$(mdblResult)
End If
Case "."
If Val(mstrDisplay) < 0 Then
mstrDisplay = "0"
Else
mdblResult = Val(mstrDisplay)
mdblResult = Sqr(mdblResult)
mstrDisplay = Format$(mdblResult)
End If
End Select
If mstrDisplay = "." Then
lblDisplay = "0."
End Sub
|
|
Question Tools |
Search this Question |
|
|
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 calculator in Visual Basic 6?
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 questions
Search
|