need help with VB 08 Calculator

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jac130
    New Member
    • Oct 2008
    • 18

    need help with VB 08 Calculator

    I need to create a calculator using combo boxes/procedures/functions etc... and some other stuff. most of it works properly, but, i need to create a user defined function that checks if both input numbers of the calculation are in fact numbers (isnumeric). the function must check both numbers, display a message box and return a value. but it crashes every time i test it - like putting in a letter instead of a number. any help would be great,
    here's my code so far: (first function is the one malfunctioning)
    Public Class form1
    Function checknumber(ByV al num1 As String, ByVal num2 As String) As Boolean
    If Not IsNumeric(num1) Then
    MsgBox("Input must be a number.")
    ElseIf Not IsNumeric(num2) Then
    MsgBox("Input must be a number.")
    Return False
    End If

    End Function
    Sub rounding(ByVal roundnum As Double)
    If txtRound.Enable d = True Then
    txtResult.Text = FormatNumber(tx tResult.Text, roundnum)
    End If
    End Sub
    Private Sub btnEquals_Click (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnEquals.Click
    Dim num1, num2, roundnum As Double
    num1 = txtNum1.Text
    num2 = txtNum2.Text
    roundnum = txtRound.Text


    If cmbOperation.Te xt = "Addition" Then
    'calculate result
    addition(num1, num2)
    End If

    If cmbOperation.Te xt = "Subtractio n" Then
    'calculate result
    subtraction(num 1, num2)
    End If

    If cmbOperation.Te xt = "Multiplication " Then
    'calculate result
    multiplication( num1, num2)
    End If

    If cmbOperation.Te xt = "Division" Then
    'calculate result
    division(num1, num2)
    End If

    If cmbOperation.Te xt = "Mod" Then
    'calculate result
    modular(num1, num2)
    End If
    If cmbOperation.Te xt = "Exponentiation " Then
    'calculate result
    exponentiate(nu m1, num2)
    End If

    'validate operation
    If cmbOperation.Te xt = "" Then
    MsgBox("no operation is selected.")
    End If
    End Sub


    Sub addition(ByVal num1 As Double, ByVal num2 As Double)
    checknumber(num 1, num2)
    txtResult.Text = (num1 + num2)
    'build list
    lstResults.Item s.Add(txtResult .Text)
    End Sub

    Sub subtraction(ByV al num1 As Double, ByVal num2 As Double)
    txtResult.Text = num1 - num2
    'build list
    lstResults.Item s.Add(txtResult .Text)
    End Sub

    Sub multiplication( ByVal num1 As Double, ByVal num2 As Double)
    txtResult.Text = num1 * num2
    'build list
    lstResults.Item s.Add(txtResult .Text)
    End Sub

    Sub division(ByVal num1 As Double, ByVal num2 As Integer)
    'validate input
    If num2 = 0 Then
    MsgBox("Number 2 cannot be 0.")
    Exit Sub
    End If
    txtResult.Text = num1 / num2
    'build list
    lstResults.Item s.Add(txtResult .Text)
    End Sub
    Sub modular(ByVal num1 As Double, ByVal num2 As Double)
    'validate input
    If num2 = 0 Then
    MsgBox("Number 2 cannot be 0.")
    Exit Sub
    End If
    txtResult.Text = num1 Mod num2
    'build list
    lstResults.Item s.Add(txtResult .Text)
    End Sub
    Sub exponentiate(By Val num1 As Double, ByVal num2 As Double)
    'validate input
    If num1 = 0 And num2 < 0 Then
    MsgBox("input is invalid.")
    Exit Sub
    End If
    txtResult.Text = num1 ^ num2
    'build list
    lstResults.Item s.Add(txtResult .Text)
    End Sub
    Private Sub btnClear_Click( ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnClear.Click
    lstResults.Item s.Clear()
    txtNum1.Text = ""
    txtNum2.Text = ""
    txtRound.Text = ""
    txtResult.Text = ""
    cmbOperation.Te xt = ""
    radRound.Checke d = False
    End Sub

    Private Sub radRound_Checke dChanged(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles radRound.Checke dChanged
    'disable rounding option unless user wants to round
    If radRound.Checke d = True Then
    'allow user to round
    txtRound.Enable d = True
    Else
    'do not allow rounding
    txtRound.Enable d = False
    End If
    End Sub

    Private Sub lstResults_Sele ctedIndexChange d(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles lstResults.Sele ctedIndexChange d
    If lstResults.Sele ctedIndex >= 0 Then
    If MsgBox("Use this number in the calculation?", MsgBoxStyle.Yes No) = MsgBoxResult.Ye s Then
    txtNum1.Text = lstResults.Text
    End If
    End If
    If lstResults.Sele ctedIndex = -1 Then
    MsgBox("no number is selected.")
    Exit Sub
    End If
    End Sub

    End Class
  • ubentook
    New Member
    • Dec 2007
    • 58

    #2
    '--
    Try removing the "Return False" code line.
    '--

    Comment

    • jac130
      New Member
      • Oct 2008
      • 18

      #3
      Originally posted by ubentook
      '--
      Try removing the "Return False" code line.
      '--
      don't functions have to return a value? besides, one of the requirements is that i return a value indicating there was a problem

      Comment

      • jg007
        Contributor
        • Mar 2008
        • 283

        #4
        the code that you have for checking the number works fine for me so I am not sure what you are doing wrong, what is the actual error message?

        when using it myself I have just created a textbox and a button and used the same textbox text for both values

        Code:
        Public Class Form1
        
            Function checknumber(ByVal num1 As String, ByVal num2 As String) As Boolean
                If Not IsNumeric(num1) Then
                    MsgBox("Input must be a number.")
                ElseIf Not IsNumeric(num2) Then
                    MsgBox("Input must be a number.")
                    Return False
                End If
            End Function
        
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                checknumber(TextBox1.Text, TextBox1.Text)
        
            End Sub
        
        End Class
        you might also want to try catching the values as they are enterend using something like this -

        Code:
         Public Sub checknumber(ByVal sender As System.Object, ByVal key As KeyPressEventArgs) Handles TextBox1.KeyPress
        
                If Not IsNumeric(key.KeyChar) Then
                    MsgBox("Input must be a number.")
                    key.Handled = True
                End If
        
            End Sub

        Comment

        • jac130
          New Member
          • Oct 2008
          • 18

          #5
          Originally posted by jg007
          the code that you have for checking the number works fine for me so I am not sure what you are doing wrong, what is the actual error message?

          when using it myself I have just created a textbox and a button and used the same textbox text for both values

          Code:
          Public Class Form1
          
              Function checknumber(ByVal num1 As String, ByVal num2 As String) As Boolean
                  If Not IsNumeric(num1) Then
                      MsgBox("Input must be a number.")
                  ElseIf Not IsNumeric(num2) Then
                      MsgBox("Input must be a number.")
                      Return False
                  End If
              End Function
          
              Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                  checknumber(TextBox1.Text, TextBox1.Text)
          
              End Sub
          
          End Class
          you might also want to try catching the values as they are enterend using something like this -

          Code:
           Public Sub checknumber(ByVal sender As System.Object, ByVal key As KeyPressEventArgs) Handles TextBox1.KeyPress
          
                  If Not IsNumeric(key.KeyChar) Then
                      MsgBox("Input must be a number.")
                      key.Handled = True
                  End If
          
              End Sub

          i dont get an error message, the program just crashes and highlights the code : num1=txtnum1.te xt, or which ever text box i put the letter into. this is my function:
          public Class Form1
          Function checknumber(ByV al num1 As String, ByVal num2 As String) As Boolean
          If Not IsNumeric(num1) Then
          MsgBox("Input must be a number.")
          ElseIf Not IsNumeric(num2) Then
          MsgBox("Input must be a number.")
          Return False
          End If
          End Function

          and this is how i called it in the addition procedure:

          Private Sub btnEquals_Click (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnEquals.Click
          Dim num1, num2 As Double
          num1 = txtNum1.Text
          num2 = txtNum2.Text

          If cmbOperation.Te xt = "Addition" Then
          checknumber(num 1, num2)
          'calculate result
          addition(num1, num2)

          Comment

          • jg007
            Contributor
            • Mar 2008
            • 283

            #6
            this might sound stupid but are you sure you are using the correct textbox name etc and you haven't put a break or something there?

            also the other thought as I am writing this is that if you are trying to assign a string to an integer you will get errors and you would need to make sure that num1 or whatever is set as a string, alternatively check if it is numeric before trying to assign it to an integer :)

            Comment

            • jac130
              New Member
              • Oct 2008
              • 18

              #7
              Originally posted by jg007
              this might sound stupid but are you sure you are using the correct textbox name etc and you haven't put a break or something there?

              also the other thought as I am writing this is that if you are trying to assign a string to an integer you will get errors and you would need to make sure that num1 or whatever is set as a string, alternatively check if it is numeric before trying to assign it to an integer :)
              yeah, the txtbox names are all correct. Every other part of the calculator works properly. maybe i'm not calling it correctly, and i have changed the parameters to "as integers" , but still it crashes and hightlights the code of which ever txtbox i put the letter into. and says "conversion from string "e" to type double is not valid"

              here's the function now, and how i've called it:


              Function checknumber(ByV al num1 As Integer, ByVal num2 As Integer) As Boolean
              If Not IsNumeric(num1) Then
              MsgBox("Input must be a number.")
              ElseIf Not IsNumeric(num2) Then
              MsgBox("Input must be a number.")
              Return False
              End If
              End Function

              Private Sub btnEquals_Click (ByVal sender As System.Object, ByVal e As System.EventArg s) Handles btnEquals.Click
              Dim num1, num2 As Double
              num1 = txtNum1.Text
              num2 = txtNum2.Text

              If cmbOperation.Te xt = "Addition" Then
              If Not checknumber(num 1, num2) Then
              Exit Sub
              End If
              'calculate result
              addition(num1, num2)

              Comment

              • jg007
                Contributor
                • Mar 2008
                • 283

                #8
                not being funny but there is your problem, you cannot convert a character to a double value!, it you read the message it will explain what is going wrong!

                you are trying to convert what is a string value ( example 'A' ) to a double which cannot be done so you need to check if it is numeric BEFORE assigning it!

                example code -

                Code:
                if isnumb ( numb1 , numb2 )  then 
                num1 = textbox1.text
                num2 = texbox2.text
                ' your code goes here
                else 
                msgbox ( "Please enter numbers only!")
                end if
                and the function -

                Code:
                function isnumb ( byval num1 as string , byval numb2 as integer )
                
                dim numericyes as boolean = false
                
                if isnumeric(num1) and isnumeric(num2) then 
                numericyes = true
                return numericyes
                else
                return  numericyes
                end if
                
                end function
                sorry I an being too lazy to load vb to confirm it but this code should be roughly correct :)

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  This thread is not only double-posted, it is also a homework question without [ CODE ] tags. A hat-trick of offenses.

                  I would ask all responders to remember that our rules prohibit such questions and that direct responses (simply providing answers) are not allowed either. Please see his other thread (VB 08 Calculator) for an example of how help should be provided to such questions when, and only when, the OP conforms to the correct format for asking for help in these circumstances.

                  Jac130, This is an official site warning. As both offenses occured before you received the first warning it will be treated as a single offense. You should be aware though, that subsequent offenses will result in a temporary ban of your account, followed by a permanent ban if further offenses occur after that.

                  Administrator.

                  Comment

                  Working...