|
-
Nov 15th, 2010, 07:58 AM
#1
Thread Starter
Addicted Member
Calculator
I want to make one text box calculator will be responsible for the calculations and show them, for example: (7 + * 8 * 12 * 6) Have a second text box is responsible for calculation and display the answer, as the calculator of windows:

My problem the first text box records the number, and execution of the addition / subtraction / multiplication / division is not added to the text box but deletes the first number written down before performing mathematical operations.
* Of course the result is going good, but just I want to see your doing the first mathematical textbox1.
Here's the code:
Code:
Option Explicit On
Public Class Form1
Dim FirstNumber As Single
Dim SecondNumber As Single
Dim AnswerNumber As Single
Dim ArithmeticProcess As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox1.Text = TextBox1.Text + "1"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = TextBox1.Text + "2"
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
TextBox1.Text = TextBox1.Text + "3"
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
TextBox1.Text = TextBox1.Text + "4"
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
TextBox1.Text = TextBox1.Text + "5"
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
TextBox1.Text = TextBox1.Text + "6"
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
TextBox1.Text = TextBox1.Text + "7"
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
TextBox1.Text = TextBox1.Text + "8"
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
TextBox1.Text = TextBox1.Text + "9"
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
TextBox1.Text = TextBox1.Text + "0"
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
TextBox1.Text = TextBox1.Text + "."
End Sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
FirstNumber = Val(TextBox1.Text)
TextBox1.Text = ""
ArithmeticProcess = "+"
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
FirstNumber = Val(TextBox1.Text)
TextBox1.Text = ""
ArithmeticProcess = "-"
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
FirstNumber = Val(TextBox1.Text)
TextBox1.Text = ""
ArithmeticProcess = "X"
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
FirstNumber = Val(TextBox1.Text)
TextBox1.Text = ""
ArithmeticProcess = "/"
End Sub
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
SecondNumber = Val(TextBox1.Text)
If ArithmeticProcess = "+" Then
AnswerNumber = FirstNumber + SecondNumber
End If
If ArithmeticProcess = "-" Then
AnswerNumber = FirstNumber - SecondNumber
End If
If ArithmeticProcess = "X" Then
AnswerNumber = FirstNumber * SecondNumber
End If
If ArithmeticProcess = "X" Then
AnswerNumber = FirstNumber * SecondNumber
End If
If ArithmeticProcess = "/" Then
AnswerNumber = FirstNumber / SecondNumber
End If
TextBox2.Text = AnswerNumber
TextBox1.Text = ""
End Sub
End Class
-
Nov 15th, 2010, 08:20 AM
#2
Hyperactive Member
Re: Calculator
You have to pass the textbox value to a global variable.
Code:
Private strValue as string
private strSign as string '+, /, -
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
strValue = strValue & " " & strSign & " " & "5"
End Sub
Last edited by hassa046; Nov 15th, 2010 at 08:23 AM.
-
Nov 15th, 2010, 08:25 AM
#3
Hyperactive Member
Re: Calculator
When you have a clear button (CE or C) you set of course the value for strValue = ""
-
Nov 15th, 2010, 10:21 AM
#4
Thread Starter
Addicted Member
Re: Calculator
do like this yes?:
Code:
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mhsvon4.Click
TextBox1.Text = TextBox1.Text + "4"
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mhsvon5.Click
strValue = strValue & " " & strSign & " " & "5"
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Mhsvon6.Click
TextBox1.Text = TextBox1.Text + "6"
End Sub
*old code
* new code (Your code)
cuz i try and i Pressed on button5 And it did nothing
Only the old code worked.
-
Nov 15th, 2010, 11:06 AM
#5
Re: Calculator
have a look at my calculator in my signature
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 15th, 2010, 12:37 PM
#6
Thread Starter
Addicted Member
Re: Calculator
 Originally Posted by .paul.
have a look at my calculator in my signature
Sorry but I did not understand how to use, I would like to use a calculator with buttons + hot keys, my problem is just what I wrote above.
-
Nov 15th, 2010, 02:19 PM
#7
Hyperactive Member
Re: Calculator
Hi, i tried to fill in the gaps. I don't have here VS so i couldn't try it. But you should be able to complete it.
Code:
Option Explicit On
Public Class Form1
private event KeyPressed
Dim FirstNumber As Single
Dim SecondNumber As Single
Dim AnswerNumber As Single
Dim ArithmeticProcess As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
strValue = strValue & "1"
raiseevent keyPressed
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
strValue = strValue & "2"
raiseevent keyPressed
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
strValue = strValue & "3"
raiseevent keyPressed
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
strValue = strValue & "4"
raiseevent keyPressed
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
strValue = strValue & "5"
raiseevent keyPressed
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
strValue = strValue & "6"
raiseevent keyPressed
End Sub
Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
strValue = strValue & "7"
raiseevent keyPressed
End Sub
Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
strValue = strValue & "8"
raiseevent keyPressed
End Sub
Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
strValue = strValue & "9"
raiseevent keyPressed
End Sub
Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
strValue = strValue & "0"
raiseevent keyPressed
End Sub
Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
strValue = strValue & "."
raiseevent keyPressed
End Sub
private sub Me_KeyPressed handles me.keypressed
Textbox1.text = strValue
end sub
Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
FirstNumber = Val(TextBox1.Text)
strValue = strValue & "+"
ArithmeticProcess = "+"
End Sub
Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
FirstNumber = Val(TextBox1.Text)
strValue = strValue & "-"
ArithmeticProcess = "-"
End Sub
Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
FirstNumber = Val(TextBox1.Text)
strValue = strValue & "X"
ArithmeticProcess = "X"
End Sub
Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
FirstNumber = Val(TextBox1.Text)
strValue = strValue & "/"
ArithmeticProcess = "/"
End Sub
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
SecondNumber = Val(TextBox1.Text)
If ArithmeticProcess = "+" Then
AnswerNumber = FirstNumber + SecondNumber
End If
If ArithmeticProcess = "-" Then
AnswerNumber = FirstNumber - SecondNumber
End If
If ArithmeticProcess = "X" Then
AnswerNumber = FirstNumber * SecondNumber
End If
If ArithmeticProcess = "X" Then
AnswerNumber = FirstNumber * SecondNumber
End If
If ArithmeticProcess = "/" Then
AnswerNumber = FirstNumber / SecondNumber
End If
TextBox2.Text = AnswerNumber
TextBox1.Text = ""
End Sub
End Class
-
Nov 15th, 2010, 03:49 PM
#8
Thread Starter
Addicted Member
Re: Calculator
It writes like I want, just the wrong result, for example: 5+7 and I got 10.
i would be happy if you help me fix it.
Thank you
-
Nov 16th, 2010, 02:01 PM
#9
Thread Starter
Addicted Member
-
Nov 16th, 2010, 02:33 PM
#10
New Member
Re: Calculator
hello, maybe it 's wrong here :
Code:
Private Sub Button17_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button17.Click
SecondNumber = Val(TextBox1.Text)
If ArithmeticProcess = "+" Then
AnswerNumber = FirstNumber + SecondNumber
End If
If ArithmeticProcess = "-" Then
AnswerNumber = FirstNumber - SecondNumber
End If
If ArithmeticProcess = "X" Then
AnswerNumber = FirstNumber * SecondNumber
End If
If ArithmeticProcess = "X" Then
AnswerNumber = FirstNumber * SecondNumber
End If
If ArithmeticProcess = "/" Then
AnswerNumber = FirstNumber / SecondNumber
End If
I see twice " If ArithmeticProcess = "X" Then
AnswerNumber = FirstNumber * SecondNumber
End If"
twice the same code after each other, can't be good !
But it also shouldn't matter (I think) for making bad calculations.
Maybe someone else knows...GL
 VB.NET 2010 - Windows XP SP3
"Computers don't lie"
If Then Else
-
Nov 16th, 2010, 02:37 PM
#11
Re: Calculator
 Originally Posted by mamrom
Sorry but I did not understand how to use, I would like to use a calculator with buttons + hot keys, my problem is just what I wrote above.
you must've been looking at the wrong link. it does what you asked for:
http://www.vbforums.com/showthread.php?t=622454
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Nov 16th, 2010, 03:53 PM
#12
Thread Starter
Addicted Member
Re: Calculator
 Originally Posted by .paul.
ohh thx
in first time i click in the other link (http://www.vbforums.com/showthread.php?t=624262)
By the way I would be happy if you help me also on this subject:
http://www.vbforums.com/showthread.php?t=633081
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|