masked textbox to numeric

Im trying to convert a textbox information to decimal like the following:

01 FIELD PIC 9(10)

...

SET FIELD TO TYPE System.Convert::ToDecimal(self::textbox1::Text)

The problem is that the textbox contains a mask, and the function tries to convert "(XX)XXXX-XXXX" to numeric, and fails.

Is there a way to make the textbox recieve only the typed information?

  • Verified Answer

    Is this a maskedtextbox control?

    If it is then you can set the property TextMaskFormat = ExcludePromptAndLiterals and then the value of the Text property will be stripped of the mask characters and the move should work.

    You might want to use TryParse instead if you wish to validate that the field contains valid characters.

          01 field     pic 9(10).
          01 numfield  decimal.
          01 mystring  string.

              set mystring to maskedtextbox1::Text
              if type Decimal::TryParse(mystring, numfield)
                 move numfield to field
              else
                invoke type MessageBox::Show("Error not numeric")
              end-if

  •       class-id WindowsFormsApplication1.Form1 is partial

                    inherits type System.Windows.Forms.Form.

          working-storage section.

          method-id NEW.

          procedure division.

              invoke self::InitializeComponent

              goback.

          end method.

          method-id button1_Click final private.

          working-storage section.

          01 aux string.

          01 FIELD PIC 9(10).

          procedure division using by value sender as object e as type System.EventArgs.

              move textBox1::Text::Replace("(", "") to aux.

              move aux::Replace(")", "") to aux.

              move aux::Replace("_", "") to aux.

              move aux::Replace("-", "") to aux.

              set FIELD to type Convert::ToInt32(aux)

              invoke type System.Windows.Forms.MessageBox::Show(FIELD::ToString())

          end method.

          end class.

  • It worked, thanks Chris !

    Also thanks for the suggestion Bencz, the replace function can be useful to me in the future