Welcome to Dream.In.Code
Become a VB.NET Expert!

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!




how to fix equal

 
Reply to this topicStart new topic

how to fix equal

suger
1 Oct, 2007 - 10:40 AM
Post #1

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 4


My Contributions
hi all,

i creat calculator by vb....

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

        End Try
    End Sub

  
End Class


so how i can fix it..and in which line
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Fix Equal
1 Oct, 2007 - 10:45 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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.

Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

suger
RE: How To Fix Equal
1 Oct, 2007 - 10:54 AM
Post #3

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 4


My Contributions
thanx for helping

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.

ist easier..and help me with detials

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Fix Equal
1 Oct, 2007 - 11:16 AM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Well if you read the code in the tutorial I use a boolean to determine if the decimal has been pushed, and other operations, so thats in the tutorial
User is offlineProfile CardPM
+Quote Post

suger
RE: How To Fix Equal
1 Oct, 2007 - 11:50 AM
Post #5

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 4


My Contributions
i try to follow the code but its seems hard

thats mean i have to change all the codes i have been worked with

what is the need for decimal ..i deal with integers only

how how >>>
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Fix Equal
1 Oct, 2007 - 11:54 AM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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.
User is offlineProfile CardPM
+Quote Post

suger
RE: How To Fix Equal
1 Oct, 2007 - 01:06 PM
Post #7

New D.I.C Head
*

Joined: 1 Oct, 2007
Posts: 4


My Contributions
could u edit my code...only for equal sign

am goin crazy..pllz SIR help me
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: How To Fix Equal
1 Oct, 2007 - 08:30 PM
Post #8

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,482



Thanked: 161 times
Dream Kudos: 9050
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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

        End Try
    End Sub


End Class


Hope this helps smile.gif
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 12:34AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month