dcsimg
Results 1 to 3 of 3
  1. #1

    Thread Starter
    Registered User
    Join Date
    Mar 2018
    Posts
    1

    Need Codes for decimal(%) and square root

    Here's my Code for my Calculator

    Design of my Calculator : https://imgur.com/j6uPREq

    im currently missing the percentage and square root codes (im still new to programming)
    Code:
        Dim Firstnum As Decimal
        Dim secondnum As Decimal
        Dim Operations As Integer
        Dim Operator_selctor As Boolean = False
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "1"
            Else
                TextBox1.Text = "1"
            End If
        End Sub
    
        Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "2"
            Else
                TextBox1.Text = "2"
            End If
        End Sub
    
        Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "3"
            Else
                TextBox1.Text = "3"
            End If
        End Sub
    
        Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "4"
            Else
                TextBox1.Text = "4"
            End If
        End Sub
    
        Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "5"
            Else
                TextBox1.Text = "5"
            End If
        End Sub
    
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "6"
            Else
                TextBox1.Text = "6"
            End If
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "7"
            Else
                TextBox1.Text = "7"
            End If
        End Sub
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "8"
            Else
                TextBox1.Text = "8"
            End If
        End Sub
    
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "9"
            Else
                TextBox1.Text = "9"
            End If
        End Sub
    
        Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
            If TextBox1.Text <> "0" Then
                TextBox1.Text += "0"
            End If
        End Sub
    
        Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
            TextBox1.Text = "0"
        End Sub
    
        Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
            If Not (TextBox1.Text.Contains(".")) Then
                TextBox1.Text += "."
            End If
        End Sub
    
        Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
            Firstnum = TextBox1.Text
            TextBox1.Text = "0"
            Operator_selctor = True
            Operations = 1 ' = +    
        End Sub
    
        Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
            Firstnum = TextBox1.Text
            TextBox1.Text = "0"
            Operator_selctor = True
            Operations = 2 ' - -
        End Sub
    
        Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
            Firstnum = TextBox1.Text
            TextBox1.Text = "0"
            Operator_selctor = True
            Operations = 3 '=x
    
        End Sub
    
        Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
            Firstnum = TextBox1.Text
            TextBox1.Text = "0"
            Operator_selctor = True
            Operations = 4 ' /
        End Sub
    
        Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
            If Operator_selctor = True Then
                secondnum = TextBox1.Text
                If Operations = 1 Then
                    TextBox1.Text = Firstnum + secondnum
                ElseIf Operations = 2 Then
                    TextBox1.Text = Firstnum - secondnum
                ElseIf Operations = 3 Then
                    TextBox1.Text = Firstnum * secondnum
                Else
                    If secondnum = 0 Then
                        TextBox1.Text = "Error!"
                    Else
                        TextBox1.Text = Firstnum / secondnum
                    End If
                End If
                Operator_selctor = False
            End If
        End Sub
    Last edited by Shaggy Hiker; Mar 4th, 2018 at 10:44 AM. Reason: Added CODE tags.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    98,909

    Re: Need Codes for decimal(%) and square root

    Firstly, please use appropriate formatting tags when posting code, for readability.
    vb.net Code:
    1. Dim Firstnum As Decimal
    2.     Dim secondnum As Decimal
    3.     Dim Operations As Integer
    4.     Dim Operator_selctor As Boolean = False
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.  
    8.     End Sub
    9.  
    10.     Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
    11.         If TextBox1.Text <> "0" Then
    12.             TextBox1.Text += "1"
    13.         Else
    14.             TextBox1.Text = "1"
    15.         End If
    16.     End Sub
    17.  
    18.     Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
    19.         If TextBox1.Text <> "0" Then
    20.             TextBox1.Text += "2"
    21.         Else
    22.             TextBox1.Text = "2"
    23.         End If
    24.     End Sub
    25.  
    26.     Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
    27.         If TextBox1.Text <> "0" Then
    28.             TextBox1.Text += "3"
    29.         Else
    30.             TextBox1.Text = "3"
    31.         End If
    32.     End Sub
    33.  
    34.     Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
    35.         If TextBox1.Text <> "0" Then
    36.             TextBox1.Text += "4"
    37.         Else
    38.             TextBox1.Text = "4"
    39.         End If
    40.     End Sub
    41.  
    42.     Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    43.         If TextBox1.Text <> "0" Then
    44.             TextBox1.Text += "5"
    45.         Else
    46.             TextBox1.Text = "5"
    47.         End If
    48.     End Sub
    49.  
    50.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    51.         If TextBox1.Text <> "0" Then
    52.             TextBox1.Text += "6"
    53.         Else
    54.             TextBox1.Text = "6"
    55.         End If
    56.     End Sub
    57.  
    58.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    59.         If TextBox1.Text <> "0" Then
    60.             TextBox1.Text += "7"
    61.         Else
    62.             TextBox1.Text = "7"
    63.         End If
    64.     End Sub
    65.  
    66.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    67.         If TextBox1.Text <> "0" Then
    68.             TextBox1.Text += "8"
    69.         Else
    70.             TextBox1.Text = "8"
    71.         End If
    72.     End Sub
    73.  
    74.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
    75.         If TextBox1.Text <> "0" Then
    76.             TextBox1.Text += "9"
    77.         Else
    78.             TextBox1.Text = "9"
    79.         End If
    80.     End Sub
    81.  
    82.     Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
    83.         If TextBox1.Text <> "0" Then
    84.             TextBox1.Text += "0"
    85.         End If
    86.     End Sub
    87.  
    88.     Private Sub Button16_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button16.Click
    89.         TextBox1.Text = "0"
    90.     End Sub
    91.  
    92.     Private Sub Button11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button11.Click
    93.         If Not (TextBox1.Text.Contains(".")) Then
    94.             TextBox1.Text += "."
    95.         End If
    96.     End Sub
    97.  
    98.     Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
    99.         Firstnum = TextBox1.Text
    100.         TextBox1.Text = "0"
    101.         Operator_selctor = True
    102.         Operations = 1 ' = +    
    103.     End Sub
    104.  
    105.     Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
    106.         Firstnum = TextBox1.Text
    107.         TextBox1.Text = "0"
    108.         Operator_selctor = True
    109.         Operations = 2 ' - -
    110.     End Sub
    111.  
    112.     Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
    113.         Firstnum = TextBox1.Text
    114.         TextBox1.Text = "0"
    115.         Operator_selctor = True
    116.         Operations = 3 '=x
    117.  
    118.     End Sub
    119.  
    120.     Private Sub Button12_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button12.Click
    121.         Firstnum = TextBox1.Text
    122.         TextBox1.Text = "0"
    123.         Operator_selctor = True
    124.         Operations = 4 ' /
    125.     End Sub
    126.  
    127.     Private Sub Button18_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button18.Click
    128.         If Operator_selctor = True Then
    129.             secondnum = TextBox1.Text
    130.             If Operations = 1 Then
    131.                 TextBox1.Text = Firstnum + secondnum
    132.             ElseIf Operations = 2 Then
    133.                 TextBox1.Text = Firstnum - secondnum
    134.             ElseIf Operations = 3 Then
    135.                 TextBox1.Text = Firstnum * secondnum
    136.             Else
    137.                 If secondnum = 0 Then
    138.                     TextBox1.Text = "Error!"
    139.                 Else
    140.                     TextBox1.Text = Firstnum / secondnum
    141.                 End If
    142.             End If
    143.             Operator_selctor = False
    144.         End If
    145.     End Sub
    Secondly, that code is basically irrelevant if it's got nothing to do with what you're actually having trouble implementing.

    With regard to percentages, it's very, VERY basic arithmetic. You would have been taught how to calculate a percentage in maths class at school. What were you taught and what problem are you having implementing that in code?

    As for square roots, look at the Math class.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,135

    Re: Need Codes for decimal(%) and square root

    As JMC pointed out, code is more readable with tags. He used the tags you get with the VB button. I edited your post to add [CODE][/CODE] tags, which you can do by pressing the # button and pasting the code between the tags.

    The square root function is Math.Sqrt(). I point this out because JMC managed to use "Math Class" to mean two different things in two different sentences. That's something I would do. but might confuse. There is a Math Class in .NET that has a bunch of math functionality, and Sqrt() is one of the methods.
    My usual boring signature: Nothing

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width


×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.