hello,
well my question is that when am creating a standard vb calculator, am unable to display the number 1,2 and so on the textbox??
How do we do that?
Thx in advance

Recommended Answers

All 8 Replies

can you show us your code? so we could see how far you been doing..

well ive try to do one but the prob is that there is 2 textbox where we can input each number in one of these then it calculates and give the result. But me i want only one textbox where we can input both number to do the calculation n then get the answer
Plz help

here is my code
Dim a As Double
Dim b As Double
Private Sub Delete_Click()
Result.Text = ""
Operation.Text = ""
Product2.Text = ""
Product1.Text = ""
End Sub

Private Sub dot_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "."
Else
Product2.Text = Product2.Text + "."
End If
End Sub

Private Sub exit_Click()
Me.Hide
End Sub

Private Sub Power_Click()
Operation.Text = "^"
End Sub

Private Sub Divide_Click()
Operation.Text = "/"
End Sub

Private Sub Equal_Click()
a = Val(Product1.Text)
b = Val(Product2.Text)
If Operation.Text = "+" Then
Result.Text = a + b
End If
If Operation.Text = "-" Then
Result.Text = a - b
End If
If Operation.Text = "*" Then
Result.Text = a * b
End If
If Operation.Text = "/" Then
If Product2.Text <> 0 Then
Result.Text = a / b
Else
Result.Text = "Cannot divide by zero"
End If
End If
End Sub

Private Sub Minus_Click()
Operation.Text = "-"
End Sub

Private Sub Multiply_Click()
Operation.Text = "*"
End Sub

Private Sub No0_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "0"
Else
Product2.Text = Product2.Text + "0"
End If
End Sub

Private Sub No1_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "1"
Else
Product2.Text = Product2.Text + "1"
End If
End Sub

Private Sub No2_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "2"
Else
Product2.Text = Product2.Text + "2"
End If
End Sub

Private Sub No3_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "3"
Else
Product2.Text = Product2.Text + "3"
End If
End Sub

Private Sub No4_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "4"
Else
Product2.Text = Product2.Text + "4"
End If
End Sub

Private Sub No5_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "5"
Else
Product2.Text = Product2.Text + "5"
End If
End Sub

Private Sub No6_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "6"
Else
Product2.Text = Product2.Text + "6"
End If
End Sub

Private Sub No7_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "7"
Else
Product2.Text = Product2.Text + "7"
End If
End Sub

Private Sub No8_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "8"
Else
Product2.Text = Product2.Text + "8"
End If
End Sub

Private Sub No9_Click()
If Operation.Text = "" Then
Product1.Text = Product1.Text + "9"
Else
Product2.Text = Product2.Text + "9"
End If
End Sub

Private Sub Plus_Click()
Operation.Text = "+"
End Sub

What is the problem you need to store the values in a variable. No need to sue second textbox.

create a textbox on your form and use the keypress event to monitor the input
keyascii=13 for carriage return 42 and 43 for plus and minus and 45 and 47 for divide and multiply, in this way, you can create complex input such as 120*67/34+5-7 moving the numbers to variables and calculate in a mathematical true fashion (you could even use brackets if necessary). press enter (keyascii=13) and clear your textbox, using it to display your results.

first create a textbox
second declare your variables

Private firstvariable As Double
Private flag As Boolean
Private secondvariable As Double
Private mathsign As Integer

third ,put this code in the keypress event of the textbox

Private Sub Text1_KeyPress(KeyAscii As Integer)

Select Case KeyAscii
Case 42, 43, 45, 47
firstvariable = Text1.Text
mathsign = KeyAscii
KeyAscii = 0
flag = True
Exit Sub
Case 13, 61
secondvariable = Text1.Text
KeyAscii = 0
calculate
Exit Sub
Case Else
If flag = True Then
Text1.Text = ""
flag = False
Exit Sub
End If
End Select
'this code allows you to enter continuous
'calculations like 123/4=*2=+7=-6= and so on
'to start a new calculation just press a number
'and the textbox is cleared (except for the new number)

'13=Return
'42=multiply
'43=addition
'45=subtraction
'47=division
'61=equals

End Sub

fourth create the following sub

Sub calculate()

Select Case mathsign
Case "45"
results = firstvariable - secondvariable
Case "43"
results = firstvariable + secondvariable
Case "47"
If secondvariable = 0 Then
Msg = "You cannot divide by Zero"
Style = vbOKOnly
Title = "Division by Zero"
response = MsgBox(Msg, Style, Title)
If response = vbOK Then
Text1.Text = ""
Exit Sub
End If
End If
results = firstvariable / secondvariable
Case 42
results = firstvariable * secondvariable
End Select
Text1.Text = results
flag = True

End Sub

tested and works fine

see this program :

Calculator.zip

Hope this help.

Im a student, can you give me some code about standard calculator according in visual basic. pm me if there have. until tomorrow morning, ill wait. march 15, 2008.
thank you.

Hi
i'm doing the same assignment. i started taking program for a month. i declared the variables already but my result still won't display when i press equal. I only have one text box to display the answer. Is it possible to give me some pointers?

here's my code so far

Public Class Form1
Dim abp As Boolean 'abp - action button pressed
Dim isDecimal As Boolean
Dim action As String
Dim num, result As Double
Dim memory As Double
Dim operation As Double

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
txtResult.Text = 0
abp = True
isDecimal = False
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
abp = True
isDecimal = False
txtResult.Text = "0"
action = "none"
End Sub

Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
abp = True
isDecimal = False
num = CDbl(txtResult.Text)
Select Case action
Case "none"
result = num
Case "+"
result += num
Case "-"
result -= num
Case "*"
result *= num
Case "/"
result /= num
End Select
End Sub

Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
num = CDbl(txtResult.Text)
If abp = True Then
txtResult.Text = "-"
abp = False
Else
'do subtraction
abp = True
isDecimal = False
action = "-"
End If

End Sub

Private Sub btnMult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMult.Click
num = CDbl(txtResult.Text)
abp = True
isDecimal = False
action = "*"
End Sub

Private Sub btnDiv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDiv.Click
num = CDbl(txtResult.Text)
abp = True
isDecimal = False
action = "/"
End Sub

Private Sub btnEqual_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEqual.Click
num = CDbl(txtResult.Text)
abp = True
isDecimal = False
action = "=" 'may be it should be action = "none"
If memory = False Then
txtResult.Text = Nothing
Else
txtResult.Text = ""

End If
Select Case operation
Case "+"
memory = txtResult.Text + num
Case "-"
memory = txtResult.Text - num
Case "/"
memory = txtResult.Text / num
Case "*"
memory = txtResult.Text * num
End Select

memory = True

End Sub

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
If abp = True Then
txtResult.Text = 1
Else
txtResult.Text = txtResult.Text & 1
End If
abp = False
End Sub

Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click
If abp = True Then
txtResult.Text = 2
Else
txtResult.Text = txtResult.Text & 2
End If
abp = False
End Sub
Private Sub btn3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click
If abp = True Then
txtResult.Text = 3
Else
txtResult.Text = txtResult.Text & 3
End If
abp = False
End Sub

Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click
If abp = True Then
txtResult.Text = 4
Else
txtResult.Text = txtResult.Text & 4
End If
abp = False
End Sub

Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click
If abp = True Then
txtResult.Text = 5
Else
txtResult.Text = txtResult.Text & 5
End If
abp = False
End Sub

Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click
If abp = True Then
txtResult.Text = 6
Else
txtResult.Text = txtResult.Text & 6
End If
abp = False
End Sub

Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click
If abp = True Then
txtResult.Text = 7
Else
txtResult.Text = txtResult.Text & 7
End If
abp = False
End Sub

Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click
If abp = True Then
txtResult.Text = 8
Else
txtResult.Text = txtResult.Text & 8
End If
abp = False
End Sub

Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click
If abp = True Then
txtResult.Text = 9
Else
txtResult.Text = txtResult.Text & 9
End If
abp = False
End Sub

Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
If abp = True Then
txtResult.Text = "0."
isDecimal = True
Else
txtResult.Text = txtResult.Text & 0
End If
abp = False
End Sub

Private Sub btnDesimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDesimal.Click
If isDecimal = False Then
If abp = True Then
txtResult.Text = "0."
Else
txtResult.Text = txtResult.Text & "."
End If
abp = False
isDecimal = True
End If
End Sub

End Class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.