3 Replies - 391 Views - Last Post: 08 April 2013 - 10:40 PM Rate Topic: -----

#1 ladyaoi  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 22-September 11

Invalid Operation Exception: Event Handling Issues

Posted 08 April 2013 - 01:05 PM

I have a form application that that data from a several textboxes and uses it to calculate a value, which is then written to a label on the form. The label has to update from the input (i.e. I can't use a button to handle the event). I thought the best way to this might be using the TextChanged Event. It worked when I tested it for a single textbox. Any change I made in the textbox reflected in the label. But when I attempted to duplicate the code for an adiitional textbox I got the invalid operation exception.

Any ideas what's wrong or am I just not approaching this problem from the right angle?

Here's some bits of the code Note* In the displayResults() there are other variables, when these variables do not have a user entered value, there are equal to 0

Private Sub calculateGas()
        calculatedGas = Convert.ToDouble(txtbxCostGas.Text) * Convert.ToDouble(txtbxGasNeeded.Text)
End Sub

Private Sub displayResults()
        calculatedTotalCost = calculatedElectric + calculatedGas + calculatedWater

        lblResult.Text = calculatedTotalCost.ToString("C2")
        lblResult.Visible = True

Private Sub textbxCostGas_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtbxCostGas.TextChanged
        calculateGas()
        displayResults()
End Sub


Is This A Good Question/Topic? 0
  • +

Replies To: Invalid Operation Exception: Event Handling Issues

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Invalid Operation Exception: Event Handling Issues

Posted 08 April 2013 - 01:54 PM

convert.ToDouble() in a textchange event (or as you're doing it, calling a sub that uses it from a textchange event) is problematic to begin with.

If the user deletes the contents of the textbox, "" is passed to the Convert.ToDouble and of course "" cannot be converted to double which throws an exception and halts operation of your program.

Use Double.TryParse instead and ensure that what is being passed can indeed be parsed to a double.

create a Function that uses double.tryparse against the 3 double variables you want to calculate with, passing each of the textboxes.Text properties

Return true if all three pass
Return False if any fail

Once this is complete, call the function in the textbox change event

If you get a true return, call your display sub passing the 3 dbl variables into it for calculation

if you get a false return, notify the user of invalid input and exit the sub

Side Note: since all three textboxes would have identical code, just combine the handles into a single textchanged event.


Another option is to use NumericUPDown controls for user input ensuring that you have a numeric value to begin with. Use the valuechanged event to pass the values into the routine.

This post has been edited by CharlieMay: 08 April 2013 - 02:00 PM

Was This Post Helpful? 1
  • +
  • -

#3 ladyaoi  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 22-September 11

Re: Invalid Operation Exception: Event Handling Issues

Posted 08 April 2013 - 02:06 PM

I will try what you said. I thought it was becoming convoluted, thank you for the suggestion. I will post back shortly with the results.
Was This Post Helpful? 0
  • +
  • -

#4 ladyaoi  Icon User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 63
  • Joined: 22-September 11

Re: Invalid Operation Exception: Event Handling Issues

Posted 08 April 2013 - 10:40 PM

Sorry I took so long. But I stopped calling a sub and just coded the calculation into the textchange handler. I did use Double.TryParse to validate the information before using it in the calculations.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1