|
-
Nov 20th, 2004, 12:48 PM
#1
Thread Starter
Member
[Solved]I need some help with a VB.NET Calculator
I have the following code for the decimal button on my calculator...
dbldisplay = CDbl(lblDisplay.Text)
lblDisplay.Text = dbldisplay & "."
This displays a decimal point on the display, but when i click another number, the decimal point disappears.
Any ideas?
Last edited by Zimpaz; Nov 20th, 2004 at 08:59 PM.
-
Nov 20th, 2004, 01:20 PM
#2
That's because "71." becomes "71"
You don't need to convert to double and then assign to text. Since the buttons only add numbers to the textbox, you only need to concatenate the strings.
For decimal,
textboxname.Text = textboxname.Text & "."
Of course, you should be checking if another . exists or not.
For other numbers, simply
textboxname.text = textboxname.text & "7"
Get it?
-
Nov 20th, 2004, 01:37 PM
#3
Thread Starter
Member
thankyou, any idea how i would go about making the + and - keys work? then the equals button
-
Nov 20th, 2004, 03:52 PM
#4
PowerPoster
Hi,
Is your next project Inventing a Wheel?
Seriously, if you want to replicate a true calculator you are not going to use the textbox.text as your calculating medium. You use that only to accept input and display results. You will have to use double type (or preferably decimal type) variables working away in the background. You will have to check each keypress and then act accordingly. e.g. if the keypress is 7 you use
arrNumber(0) =7
arrResult(0) = 7
if the next keypress is 3 you use
arrNumber(0)=arrNumber(0)*10+3
arrResult(0)=arrResult(0)*10+3
etc. Obviously you have to keep a record of the numbers to decide which array element to use.
if the next keypress is + you use
arrOperand(0)="+"
and if your next number is 8
arrNumber(1)=8
and use select case to put the correct result into
arrResult(1)
If the next key press is = you put the value of arrResult(1) into dTemp
But if the keypress was + - you have to continue with the previous sequence.
That bit is the easiest part.
If a keypress is * / or ^ you have to be prepared to stick to the rules of arithmetic and cancel the previous action (unless it was an = ) and come back to it when you have completed the priority operation.
Get the idea?
I hope I have said enough to persuade you to forget the idea and go out and buy a calculator. If not we will discuss square roots etc
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Nov 20th, 2004, 04:02 PM
#5
Thread Starter
Member
It is for a university assignment. We are supposed to be using variables and loops i think.
-
Nov 20th, 2004, 08:56 PM
#6
PowerPoster
Hi,
Check and find out if you are required to observe the rules of arithmetic. Some very basic calculators do not. They just carry out each instruction sequentially.
e.g. Following the rules 3+4*5 =23 but sequentially the result is 35.
If you don't have to follow the rules then the project is much simplified, but please don't expect me to use it as a calculator
Have you followed what I posted?
Last edited by taxes; Nov 20th, 2004 at 09:00 PM.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Nov 20th, 2004, 09:00 PM
#7
Thread Starter
Member
Thanks for your help guys, i have managed to bodge together some code that works. Expect me back next time i have an assignment :0)
Zimpaz
-
Nov 20th, 2004, 09:01 PM
#8
PowerPoster
Hi,
It would be interesting to see your code. I thought it would take much longer.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Nov 20th, 2004, 09:05 PM
#9
Thread Starter
Member
Its only a basic calculator as it can only add, take away, divide or multiplay 2 numbers. Here is the code for your enjoyment
Dim number1, number2, operator As String
Private Sub btnZero_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZero.Click
lblDisplay.Text = lblDisplay.Text & "0"
End Sub
Private Sub btnOne_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOne.Click
lblDisplay.Text = lblDisplay.Text & "1"
End Sub
Private Sub btnTwo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTwo.Click
lblDisplay.Text = lblDisplay.Text & "2"
End Sub
Private Sub btnThree_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThree.Click
lblDisplay.Text = lblDisplay.Text & "3"
End Sub
Private Sub btnFour_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFour.Click
lblDisplay.Text = lblDisplay.Text & "4"
End Sub
Private Sub btnFive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFive.Click
lblDisplay.Text = lblDisplay.Text & "5"
End Sub
Private Sub btnSix_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSix.Click
lblDisplay.Text = lblDisplay.Text & "6"
End Sub
Private Sub btnSeven_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeven.Click
lblDisplay.Text = lblDisplay.Text & "7"
End Sub
Private Sub btnEight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEight.Click
lblDisplay.Text = lblDisplay.Text & "8"
End Sub
Private Sub btnNine_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNine.Click
lblDisplay.Text = lblDisplay.Text & "9"
End Sub
Private Sub btnDecimal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDecimal.Click
lblDisplay.Text = lblDisplay.Text & "."
End Sub
Private Sub btnPlus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPlus.Click
number1 = lblDisplay.Text
operator = "+"
lblDisplay.Text = ""
End Sub
Private Sub btnMinus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMinus.Click
number1 = lblDisplay.Text
operator = "-"
lblDisplay.Text = ""
End Sub
Private Sub btnMultiply_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMultiply.Click
number1 = lblDisplay.Text
operator = "*"
lblDisplay.Text = ""
End Sub
Private Sub btnDivide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDivide.Click
number1 = lblDisplay.Text
operator = "/"
lblDisplay.Text = ""
End Sub
Private Sub btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
number2 = lblDisplay.Text
If operator = "+" Then
lblDisplay.Text = (Val(number1) + Val(number2))
ElseIf operator = "*" Then
lblDisplay.Text = (Val(number1) * Val(number2))
ElseIf operator = "-" Then
lblDisplay.Text = (Val(number1) - Val(number2))
ElseIf operator = "/" Then
lblDisplay.Text = (Val(number1) / Val(number2))
End If
End Sub
Private Sub btnC_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnC.Click
lblDisplay.Text() = ""
number1 = ""
number2 = ""
operator = ""
End Sub
Private Sub btnCE_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCE.Click
lblDisplay.Text() = ""
End Sub
Private Sub btnSign_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSign.Click
Dim intpos As Integer
intpos = InStr(lblDisplay.Text, "-")
If intpos = 0 Then
lblDisplay.Text() = "-" & lblDisplay.Text
Else
lblDisplay.Text() = Val(lblDisplay.Text) * -1
End If
End Sub
End Class
-
Nov 20th, 2004, 09:21 PM
#10
PowerPoster
Hi,
Fine. You could reduce the code by having one sub with several handles to respond to the numerical button click events. Something like
Private Sub btnNumbers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnZero.Click, btnOne.Click .........etc
lblDisplay.Text = lblDisplay.Text & CStr(CType(sender, Button).Text)
End Sub
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Nov 20th, 2004, 09:25 PM
#11
PowerPoster
Hi,
Also, your C & CE buttons need to clear the appropriate variables as well as the textbox.
Taxes
The more I learn about VB.NET the more I like dBaseIII Plus
The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.
-
Nov 20th, 2004, 09:28 PM
#12
Thread Starter
Member
they do, C clears the display, and the 3 variables. CE only clears the display because it means clear entry
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
|