VB 08 Calculator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jac130
    New Member
    • Oct 2008
    • 18

    VB 08 Calculator

    i have to make a calculator in visual basic, it's slightly more complex one, using a combo box/procedures/functions. Anyway, i need to create a user defined function to make sure both input numbers (of the the calculation) are in fact numbers, the isnumeric function. both numbers have to be checked in the same function, and i must return a msgbox (as part of the function) displaying an error and return a value indicating there was a problem. it always crashes when i test it - entering a letter instead of a number into either of the textboxes. here's my code so far: the first function is the function that's malfunctioning. any help would be great
    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 btnEquals_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEquals.Click
            Dim num1, num2 As Double
            num1 = CStr(txtNum1.Text)
            num2 = CStr(txtNum2.Text)
            
            If cmbOperation.Text = "Addition" Then
                checknumber(num1, num2)
                'calculate result
                addition(num1, num2)
            End If
    
            If cmbOperation.Text = "Subtraction" Then
                'calculate result
                subtraction(num1, num2)
            End If
    
            If cmbOperation.Text = "Multiplication" Then
                'calculate result
                multiplication(num1, num2)
            End If
    
            If cmbOperation.Text = "Division" Then
                'calculate result
                division(num1, num2)
            End If
    
            If cmbOperation.Text = "Mod" Then
                'calculate result
                modular(num1, num2)
            End If
            If cmbOperation.Text = "Exponentiation" Then
                'calculate result
                exponentiate(num1, num2)
            End If
    
            'validate operation 
            If cmbOperation.Text = "" Then
                MsgBox("no operation is selected.")
            End If
        End Sub
    
    
        Sub addition(ByVal num1 As Double, ByVal num2 As Double)
            checknumber(num1, num2)
            txtResult.Text = (num1 + num2)
            'build list
            lstResults.Items.Add(txtResult.Text)
        End Sub
    
        Sub subtraction(ByVal num1 As Double, ByVal num2 As Double)
            txtResult.Text = num1 - num2
            'build list
            lstResults.Items.Add(txtResult.Text)
        End Sub
    
        Sub multiplication(ByVal num1 As Double, ByVal num2 As Double)
            txtResult.Text = num1 * num2
            'build list
            lstResults.Items.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.Items.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.Items.Add(txtResult.Text)
        End Sub
        Sub exponentiate(ByVal 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.Items.Add(txtResult.Text)
        End Sub
        Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click
            lstResults.Items.Clear()
            txtNum1.Text = ""
            txtNum2.Text = ""
            txtRound.Text = ""
            txtResult.Text = ""
            cmbOperation.Text = ""
            radRound.Checked = False
        End Sub
    
        Private Sub radRound_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radRound.CheckedChanged
            'disable rounding option unless user wants to round
            If radRound.Checked = True Then
                'allow user to round
                txtRound.Enabled = True
            Else
                'do not allow rounding
                txtRound.Enabled = False
            End If
        End Sub
    
        Private Sub lstResults_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstResults.SelectedIndexChanged
            If lstResults.SelectedIndex >= 0 Then
                If MsgBox("Use this number in the calculation?", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                    txtNum1.Text = lstResults.Text
                End If
            End If
            If lstResults.SelectedIndex = -1 Then
                MsgBox("no number is selected.")
                Exit Sub
            End If
        End Sub
    End Class
    Last edited by NeoPa; Oct 10 '08, 11:56 PM. Reason: Please remember to use the [CODE] tags provided
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32633

    #2
    Homework Assignment

    The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

    Administrator.

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32633

      #3
      I have also moved this to the appropriate forum for VB questions. You posted it in the Access / VBA forum.

      Comment

      • jac130
        New Member
        • Oct 2008
        • 18

        #4
        Originally posted by NeoPa
        Homework Assignment

        The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

        Administrator.
        i did attempt it myself, where do you think all that code came from. i only asked about the user defined function validating the input as a number.

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          I have no argument with what you say, as far as it goes.

          If you read my post carefully though (in your position I would have) you will see that it also mentions posting questions regarding any difficulties that you may have come across. All your question stated was that your function wasn't working. There is nowhere any description of the difficulties you encountered. What you attempted to do to resolve your initially observed problem.

          As a student (of whatever form) we are not prepared to help sabotage your learning process by allowing you (or anyone in your position) simply to drop their problem on the forum and expect solutions to be provided. If you read our rules in the readily available Help link, you will see why we take this stance with students particularly.

          We are more than willing to help, but for you we would rather help you to learn, than simply do it for you.

          Now, I am happy to help you with this (as I've already become involved) although I am a VBA man more than VB. I don't imagine this will be too much of a block though. At this level they are very similar I understand. I would expect you to start with explaining exactly what goes wrong, and what, if anything, are your thoughts on why this is happening.

          Alternatively you can wait for a more specialist VB expert to help. Either way, you should know we would expect you to drive. We would only be there to give guidance.

          Comment

          • jac130
            New Member
            • Oct 2008
            • 18

            #6
            the rest of the calculator works, but when i type a letter into either of the textboxes and try to calculate it, the program crashes. I have no idea why this happens. the code looks fine to me. i need it to validate that the inputs are numbers but it just crahses.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Code:
              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
              Ok, What sort of crash is it. I'm assuming degree level here - is that right?
              A shool pupil can possibly get away with just "it crashes."

              I need to know WHERE it crashes and what the error message was if there was one. Describe clearly what happens or I can't help you.

              PS. I asssume this code has compiled successfully.

              Comment

              • jg007
                Contributor
                • Mar 2008
                • 283

                #8
                can you merge the two threads here please.

                ** Admin edit - Spoonfeeding help removed **

                Comment

                • NeoPa
                  Recognized Expert Moderator MVP
                  • Oct 2006
                  • 32633

                  #9
                  Sorry to cut your help out there JG. We have to be careful how we handle homework type questions though, and simply feeding answers is not acceptable (although I very much sympathise with - and applaud - your wanting to help).

                  I have locked the other thread (need help with VB 08 Calculator) you referred to, as explained in that thread.

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32633

                    #10
                    I should add that you are, of course, free to offer further assistance, but with the proviso that you extract the solution from the OP themselves, rather than provide it for them.

                    Possibly direct them where to look, but don't give out answers directly. This doesn't help them (or anyone really) in the long run.

                    Comment

                    • NeoPa
                      Recognized Expert Moderator MVP
                      • Oct 2006
                      • 32633

                      #11
                      I've just been told that, as this is in VB 08, it must be a .NET question rather than a VB one.

                      I am moving it to the .NET forum.

                      Comment

                      Working...