Join 149,617 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,856 people online right now. Registration is fast and FREE... Join Now!
but i need ur help to tell my how i can prevent equal button from doubling the result i tried hard to figure the solution..but i fall
to illustrate my qustion, when i click the equal button it give me a right answer BUT if i click on it again its give me another answer
i need it to give my ONLY one answer
so can u help me..and i will be thankful
really i need the answer for the next few houre to sumbit my report.
this is my code:-
CODE
Public Class Form1 Inherits System.Windows.Forms.Form Dim total1 As Integer Dim total2 As Integer Dim clearDisplay As Boolean Dim Operand1 As Double, Operand2 As Double Dim [Operator] As String
Private Sub command1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlus.Click total1 = total2 + Val(txtDisplay.Text) Operand1 = total1 'Val(txtDisplay.Text) [Operator] = "+" txtDisplay.Text = txtDisplay.Text & cmdPlus.Text clearDisplay = True txtDisplay.Clear()
End Sub
Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
txtDisplay.Text = txtDisplay.Text & btn0.Text
End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click txtDisplay.Text = txtDisplay.Text & btn1.Text End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click txtDisplay.Text = txtDisplay.Text & btn2.Text End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click txtDisplay.Text = txtDisplay.Text & btn3.Text End Sub Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click txtDisplay.Text = txtDisplay.Text & btn4.Text End Sub
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click txtDisplay.Text = txtDisplay.Text & btn5.Text End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click txtDisplay.Text = txtDisplay.Text & btn6.Text End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click txtDisplay.Text = txtDisplay.Text & btn7.Text End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click txtDisplay.Text = txtDisplay.Text & btn8.Text End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click txtDisplay.Text = txtDisplay.Text & btn9.Text End Sub
Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click txtDisplay.Text = ""
End Sub
Private Sub command2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEquals.Click
Dim result As Double
Operand2 = Val(txtDisplay.Text) Try Select Case [Operator] Case "+" result = Operand1 + Operand2 End Select Finally txtDisplay.Text = result clearDisplay = True
This looks like VB.Net and belongs in the VB.Net Forum, not here (don't double post it Ill get someone to move it), heres a tutorial I wrote on Creating a calculator in VB.Net.
what about using a boolean variable defined to indicate if the button has been clicked once, then you can decide whether or not to repeat the operation.
Well in every calculator in the world you can enter 1.23, but you cant enter 1.23.34 Just use my boolean logic to see when a button has been pressed, like when the equals button has been pressed trip the boolean to true, then don't let the equals button be pressed again until the boolean flag is false.
When button other than the equals is pressed trip the boolean flag to false.
Normally I dont do this, but you read the tutorial and didnt understand it, you posted code showing you at least tried, so I believe this code will now do what you want it to do:
CODE
Public Class Form1 Inherits System.Windows.Forms.Form Dim total1 As Integer Dim total2 As Integer Dim clearDisplay As Boolean Dim Operand1 As Double, Operand2 As Double Dim [Operator] As String 'Boolean value to determine if pressing 'the equal button does any calculations 'This flag is tripped to False each time a key is pressed 'It is tripped in the equal key click event only if 'calculations are performed, meaning it was ok to 'click the button Dim equalPressed As Boolean = True
Private Sub command1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdPlus.Click total1 = total2 + Val(txtDisplay.Text) Operand1 = total1 'Val(txtDisplay.Text) [Operator] = "+" txtDisplay.Text = txtDisplay.Text & cmdPlus.Text clearDisplay = True txtDisplay.Clear()
End Sub
Private Sub btn0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn0.Click
txtDisplay.Text = txtDisplay.Text & btn0.Text equalPressed = False End Sub
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click txtDisplay.Text = txtDisplay.Text & btn1.Text equalPressed = False End Sub
Private Sub btn2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn2.Click txtDisplay.Text = txtDisplay.Text & btn2.Text equalPressed = False End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn3.Click txtDisplay.Text = txtDisplay.Text & btn3.Text equalPressed = False End Sub Private Sub btn4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn4.Click txtDisplay.Text = txtDisplay.Text & btn4.Text equalPressed = False End Sub
Private Sub btn5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn5.Click txtDisplay.Text = txtDisplay.Text & btn5.Text equalPressed = False End Sub
Private Sub btn6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn6.Click txtDisplay.Text = txtDisplay.Text & btn6.Text equalPressed = False End Sub
Private Sub btn7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn7.Click txtDisplay.Text = txtDisplay.Text & btn7.Text equalPressed = False End Sub
Private Sub btn8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn8.Click txtDisplay.Text = txtDisplay.Text & btn8.Text equalPressed = False End Sub
Private Sub btn9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn9.Click txtDisplay.Text = txtDisplay.Text & btn9.Text equalPressed = False End Sub
Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click txtDisplay.Text = "" equalPressed = False End Sub
Private Sub command2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdEquals.Click
Dim result As Double Operand2 = Val(txtDisplay.Text) Try 'Check the value of the boolean created 'to determine if the equal button was pressed 'If it's False then proceed with the calculations If Not equalPressed Then Select Case [Operator] Case "+" result = Operand1 + Operand2 End Select equalPressed = True End If
Finally txtDisplay.Text = result clearDisplay = True