Results 1 to 12 of 12

Thread: Calculator

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    200

    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

  2. #2
    Hyperactive Member hassa046's Avatar
    Join Date
    May 2001
    Location
    Venlo, The Netherlands
    Posts
    336

    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.
    Better to regret things you did, than those you didn't
    International Intelligence

  3. #3
    Hyperactive Member hassa046's Avatar
    Join Date
    May 2001
    Location
    Venlo, The Netherlands
    Posts
    336

    Re: Calculator

    When you have a clear button (CE or C) you set of course the value for strValue = ""
    Better to regret things you did, than those you didn't
    International Intelligence

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    200

    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.

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Calculator

    have a look at my calculator in my signature

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    200

    Re: Calculator

    Quote Originally Posted by .paul. View Post
    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.

  7. #7
    Hyperactive Member hassa046's Avatar
    Join Date
    May 2001
    Location
    Venlo, The Netherlands
    Posts
    336

    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
    Better to regret things you did, than those you didn't
    International Intelligence

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    200

    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

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    200

    Re: Calculator

    Help..

  10. #10
    New Member San76's Avatar
    Join Date
    Dec 2006
    Location
    The Netherlands
    Posts
    7

    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

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Calculator

    Quote Originally Posted by mamrom View Post
    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

  12. #12

    Thread Starter
    Addicted Member
    Join Date
    Sep 2010
    Posts
    200

    Re: Calculator

    Quote Originally Posted by .paul. View Post
    you must've been looking at the wrong link. it does what you asked for:

    http://www.vbforums.com/showthread.php?t=622454
    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
  •  



Click Here to Expand Forum to Full Width