Jump to content

I need help my calculator can't calculate pass 10 and it mulitplys it own number, im using visual basic 2010

 

 

Public Class Form1
 
    Dim Firstnumber As Single
    Dim Secondnumber As Single
    Dim Answernumber As Single
    Dim Arithmeticprocess As String
 
 
 
    Private Sub num1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num1.Click
 
        textbox.Text = textbox.Text & 1
    End Sub
 
    Private Sub num2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num2.Click
        textbox.Text = textbox.Text & 2
    End Sub
 
    Private Sub num3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num3.Click
        textbox.Text = textbox.Text & 3
    End Sub
 
    Private Sub num4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num4.Click
        textbox.Text = textbox.Text & 4
    End Sub
 
    Private Sub num5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num5.Click
        textbox.Text = textbox.Text & 5
    End Sub
 
    Private Sub num6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num6.Click
        textbox.Text = textbox.Text & 6
    End Sub
 
    Private Sub num7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num7.Click
        textbox.Text = textbox.Text & 7
    End Sub
 
    Private Sub num8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num8.Click
        textbox.Text = textbox.Text & 8
    End Sub
 
    Private Sub num9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num9.Click
        textbox.Text = textbox.Text & 9
    End Sub
 
    Private Sub num0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles num0.Click
        textbox.Text = textbox.Text & 0
    End Sub
 
    Private Sub plus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles plus.Click
        Firstnumber = Val(textbox.Text)
        textbox.Text = textbox.Text & "+"
        Arithmeticprocess = "+"
    End Sub
 
    Private Sub minus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles minus.Click
        Firstnumber = Val(textbox.Text)
        textbox.Text = textbox.Text & "-"
        Arithmeticprocess = "-"
 
    End Sub
 
    Private Sub multi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles multi.Click
        Firstnumber = Val(textbox.Text)
        textbox.Text = textbox.Text & "*"
        Arithmeticprocess = "*"
    End Sub
 
    Private Sub equal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles equal.Click
        Secondnumber = Val(textbox.Text)
 
        If Arithmeticprocess = "+" Then
            Answernumber = Firstnumber + Secondnumber
        End If
        If Arithmeticprocess = "-" Then
            Answernumber = Firstnumber - Secondnumber
        End If
        If Arithmeticprocess = "*" Then
            Answernumber = Firstnumber * Secondnumber
        End If
        If Arithmeticprocess = "/" Then
            Answernumber = Firstnumber / Secondnumber
        End If
        If Arithmeticprocess = "%" Then
            Answernumber = Firstnumber / Secondnumber * 100
 
        End If
        textbox.Text = Answernumber
        Arithmeticprocess = "="
 
    End Sub
 
    Private Sub divide_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles divide.Click
        Firstnumber = Val(textbox.Text)
        textbox.Text = Text
        Arithmeticprocess = "/"
    End Sub
 
    Private Sub percent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles percent.Click
        Firstnumber = Val(textbox.Text)
        textbox.Text = Text
        Arithmeticprocess = "%"
    End Sub
 
    Private Sub root_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles root.Click
        Firstnumber = Val(textbox.Text)
        textbox.Text = Text
        Arithmeticprocess = "√"
    End Sub
 
    Private Sub square_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles square.Click
        Firstnumber = Val(textbox.Text)
        textbox.Text = Text
        Arithmeticprocess = "^2"
    End Sub
 
    Private Sub textbox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textbox.TextChanged
 
    End Sub
 
    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Clear.Click
        If Val(textbox.Text) >= 0 Then
            textbox.Text = ""
        End If
    End Sub
End Class
 

 

 

 

 

please help :)

Link to comment
https://linustechtips.com/topic/14556-calculator-help/
Share on other sites

Link to post
Share on other sites

Mrcrysis, I believe your main problem lies in getting Secondnumber.

 

Notice that if you type in 111*200 in your calculator program, when you type 111* it will call val("111*") = 111 which is what you want.  The issue arrises when you hit equals, as you are using textbox to get the second number.  With the textbox as the second number your textbox is 111*200 and calls val("111*200") = 111 which is not what you want.

 

From what I see there are a few ways to solve this....I'll give an example of the easiest way.

    Dim tmpNumber As Single       Private Sub num1_Click......        tmpNumber = tmpNumber * 10 + 1 'Holds the current number being typed in        ..........    Private Sub equal_Click.......        Secondnumber = tmpNumber        tmpNumber = 0     Private Sub plus_Click......        Firstnumber = tmpNumber        tmpNumber = 0        .....

Basically the changes made compared to your code is it introduces one extra variable to store the number as it is being typed in.  This way you can have anything written in the textbox without messing up the firstnumber or secondnumber.

0b10111010 10101101 11110000 00001101

Link to comment
https://linustechtips.com/topic/14556-calculator-help/#findComment-162330
Share on other sites

Link to post
Share on other sites

Mrcrysis, I believe your main problem lies in getting Secondnumber.

 

Notice that if you type in 111*200 in your calculator program, when you type 111* it will call val("111*") = 111 which is what you want.  The issue arrises when you hit equals, as you are using textbox to get the second number.  With the textbox as the second number your textbox is 111*200 and calls val("111*200") = 111 which is not what you want.

 

From what I see there are a few ways to solve this....I'll give an example of the easiest way.

    Dim tmpNumber As Single       Private Sub num1_Click......        tmpNumber = tmpNumber * 10 + 1 'Holds the current number being typed in        ..........    Private Sub equal_Click.......        Secondnumber = tmpNumber        tmpNumber = 0     Private Sub plus_Click......        Firstnumber = tmpNumber        tmpNumber = 0        .....

Basically the changes made compared to your code is it introduces one extra variable to store the number as it is being typed in.  This way you can have anything written in the textbox without messing up the firstnumber or secondnumber.

Sorry for the late reply but thanks so much! :) 

Link to comment
https://linustechtips.com/topic/14556-calculator-help/#findComment-179348
Share on other sites

Link to post
Share on other sites

Also a good idea to use "double" rather than "single" for number types

Double can hold much larger (and smaller) numbers

 

 

Double (8 bytes)

-1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values;

4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values

 

Single (4 bytes)

-3.4028235E+38 through -1.401298E-45 for negative values;

1.401298E-45 through 3.4028235E+38 for positive values

 

 

http://msdn.microsoft.com/en-us/library/47zceaw7%28v=vs.80%29.aspx

Corsair 400R / Intel Core i7 2600K / MSI P67A-GD53 / 2 X MSI GeForce GTX 560Ti Twin Frozr II SLI / Crucial Ballistix Sport 8GB (2x4GB) 1600Mhz


OCZ ZT 550W / Coolermaster Hyper 212 Evo / Crucial M4 128GB (2x64GB RAID 0) / WD Caviar Green 1TB / Arctic Silver 5

Link to comment
https://linustechtips.com/topic/14556-calculator-help/#findComment-185394
Share on other sites

Link to post
Share on other sites

Also a good idea to use "double" rather than "single" for number types

Double can hold much larger (and smaller) numbers

 

 

Double (8 bytes)

-1.79769313486231570E+308 through -4.94065645841246544E-324 for negative values;

4.94065645841246544E-324 through 1.79769313486231570E+308 for positive values

 

Single (4 bytes)

-3.4028235E+38 through -1.401298E-45 for negative values;

1.401298E-45 through 3.4028235E+38 for positive values

 

 

http://msdn.microsoft.com/en-us/library/47zceaw7%28v=vs.80%29.aspx

 

It use to be double before i starting messing around i thought single was the fix but clearly not :P  Have you had much experience with visual basic?

Link to comment
https://linustechtips.com/topic/14556-calculator-help/#findComment-185497
Share on other sites

Link to post
Share on other sites

It use to be double before i starting messing around i thought single was the fix but clearly not :P  Have you had much experience with visual basic?

 

Ah :)

Quite a bit yea, mainly Console Applications but also made a few Windows Forms.

After VB I moved to C# and have made some more complex things

I think VB was a great learner language though to get used to the format and syntax

Corsair 400R / Intel Core i7 2600K / MSI P67A-GD53 / 2 X MSI GeForce GTX 560Ti Twin Frozr II SLI / Crucial Ballistix Sport 8GB (2x4GB) 1600Mhz


OCZ ZT 550W / Coolermaster Hyper 212 Evo / Crucial M4 128GB (2x64GB RAID 0) / WD Caviar Green 1TB / Arctic Silver 5

Link to comment
https://linustechtips.com/topic/14556-calculator-help/#findComment-186558
Share on other sites

Link to post
Share on other sites

Ah :)

Quite a bit yea, mainly Console Applications but also made a few Windows Forms.

After VB I moved to C# and have made some more complex things

I think VB was a great learner language though to get used to the format and syntax

 

 

Im studying it right now and i think its great is C# a big step up do you know? :) 

Link to comment
https://linustechtips.com/topic/14556-calculator-help/#findComment-186658
Share on other sites

Link to post
Share on other sites

Im studying it right now and i think its great is C# a big step up do you know? :) 

Not too big step up

I switched to it to do a Computing project and had to learn it by myself from knowing VB

Uses fairly similar format, just need to remember to end lines with semicolons (;) and how to set up if statements and for statements which have slightly different format

Corsair 400R / Intel Core i7 2600K / MSI P67A-GD53 / 2 X MSI GeForce GTX 560Ti Twin Frozr II SLI / Crucial Ballistix Sport 8GB (2x4GB) 1600Mhz


OCZ ZT 550W / Coolermaster Hyper 212 Evo / Crucial M4 128GB (2x64GB RAID 0) / WD Caviar Green 1TB / Arctic Silver 5

Link to comment
https://linustechtips.com/topic/14556-calculator-help/#findComment-186763
Share on other sites

Link to post
Share on other sites

Not too big step up

I switched to it to do a Computing project and had to learn it by myself from knowing VB

Uses fairly similar format, just need to remember to end lines with semicolons ( ;) and how to set up if statements and for statements which have slightly different format

I keep forgetting the semicolons now ! D: 

Link to comment
https://linustechtips.com/topic/14556-calculator-help/#findComment-186823
Share on other sites

Link to post
Share on other sites

I keep forgetting the semicolons now ! D: 

Haha, does take a bit of time to get used to :)

Corsair 400R / Intel Core i7 2600K / MSI P67A-GD53 / 2 X MSI GeForce GTX 560Ti Twin Frozr II SLI / Crucial Ballistix Sport 8GB (2x4GB) 1600Mhz


OCZ ZT 550W / Coolermaster Hyper 212 Evo / Crucial M4 128GB (2x64GB RAID 0) / WD Caviar Green 1TB / Arctic Silver 5

Link to comment
https://linustechtips.com/topic/14556-calculator-help/#findComment-186912
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×