Highlighted
Absent Member.
Absent Member.
2471 views

masked textbox to numeric

Jump to solution

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?

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: masked textbox to numeric

Jump to solution

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

View solution in original post

0 Likes
3 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: masked textbox to numeric

Jump to solution

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

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: masked textbox to numeric

Jump to solution

      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.

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: masked textbox to numeric

Jump to solution

It worked, thanks Chris !

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

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.