locked
disabled textbox string error RRS feed

  • Question

  • User1975938630 posted

    I have multiple check boxes, each are associated with it's own text box, when the checkbox is unchecked, the textbox is not enabled and when it's checked, the text box becomes enabled so the user can enter data into it.  But if I run the program with one or more of the textboxes not enabled (the user didn't need to enter anything in that textbox) I get a "conversion from string "" to type decimal is not valid".  I can't figure out how to get around this, I want the program to still run if one or more of the textboxes aren't enabled

    I tried using an If Then statement with textbox1.Text = String.Empty statement but I still received the error.

    I'm using visual basic 2012 and I'm very new to this so i've pretty much exhausted my knowledge on what to try.  I'd rather not have to remove the checkboxes and just use the textboxes.  Any help or ideas would be greatly appreciated.

    Sunday, June 22, 2014 8:14 PM

Answers

  • User-1716253493 posted

    Without If Then, you can do like this

            Dim b As Boolean
            Dim myDec As Decimal
            b = Decimal.TryParse(TextBox1.Text, myDec)

    TryParse will try to parse textbox text into myDec, if fail myDec become 0

    the tryparse results is true/false indicate the parse fail or success

    Whatever text value you will get myDec with decimal value or zero without error

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 22, 2014 11:05 PM

All replies

  • User-1360095595 posted

    Somewhere in your code you're trying to convert a string to a decimal value, yes? At that point, just beforethe conversion, you need to add a null/empty check. 

    Sunday, June 22, 2014 8:20 PM
  • User-1716253493 posted

    Without If Then, you can do like this

            Dim b As Boolean
            Dim myDec As Decimal
            b = Decimal.TryParse(TextBox1.Text, myDec)

    TryParse will try to parse textbox text into myDec, if fail myDec become 0

    the tryparse results is true/false indicate the parse fail or success

    Whatever text value you will get myDec with decimal value or zero without error

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, June 22, 2014 11:05 PM