TextBox with currency format

Hi, I whant to know, how I can make the currency format to textbox, like of Cobol

77  Invoice-Amount      Pic $$,$$9.99.

I tryed other why, but is so strange and not so functional..., it's possible Automated teller machine ???

for example: the user will be type this number 5124.25

the user types 5: 0,05

the user types 1: 0,51

the user types 2: 5,12

the user types 4: 51,24

the user types 2: 512,42

the user types 5: 5124,25

Parents
  • Verified Answer

    I believe that I got this to work correctly by using the following example which uses the TextChanged event of the textbox.

     

           method-id textBox1_TextChanged final private.
           procedure division using by value sender as object e as type System.EventArgs.
            *>Remove previous formatting, or the decimal check will fail including leading zeros
               declare myvalue as string = 
               textBox1::Text::Replace(",", "")::Replace("$", "")::Replace(".", "")::TrimStart('0')
               declare ul as decimal
            *>Check we are indeed handling a number
               if type Decimal::TryParse(myvalue, ul)
                  set ul to ul / 100
            *>Unsub the event so we don't enter a loop
                  invoke textBox1::remove_TextChanged(new System.EventHandler(self::textBox1_TextChanged))
            *>Format the text as currency
                  set textBox1::Text to type 
                  String::Format(type System.Globalization.CultureInfo::CreateSpecificCulture("en-US"), "{0:C2}", ul)
                  invoke textBox1::add_TextChanged(new System.EventHandler(self::textBox1_TextChanged))
                  invoke textBox1::Select(textBox1::Text::Length, 0)
               end-if
               declare goodToGo as condition-value = self::TextisValid(textBox1::Text)
               if not goodToGo
                  set textBox1::Text to "$0.00"
                  invoke textBox1::Select(textBox1::Text::Length, 0)
               end-if
               goback.
        
           end method.
           method-id TextisValid private.
           procedure division using by value mytext as string
                              returning ret-bool as condition-value.
               declare money as type System.Text.RegularExpressions.Regex = 
                  new System.Text.RegularExpressions.Regex("^\$(\d{1,3}(\,\d{3})*|(\d ))(\.\d{2})?$")
               set ret-bool to money::IsMatch(mytext)
               goback.
           end method.
           
    
Reply
  • Verified Answer

    I believe that I got this to work correctly by using the following example which uses the TextChanged event of the textbox.

     

           method-id textBox1_TextChanged final private.
           procedure division using by value sender as object e as type System.EventArgs.
            *>Remove previous formatting, or the decimal check will fail including leading zeros
               declare myvalue as string = 
               textBox1::Text::Replace(",", "")::Replace("$", "")::Replace(".", "")::TrimStart('0')
               declare ul as decimal
            *>Check we are indeed handling a number
               if type Decimal::TryParse(myvalue, ul)
                  set ul to ul / 100
            *>Unsub the event so we don't enter a loop
                  invoke textBox1::remove_TextChanged(new System.EventHandler(self::textBox1_TextChanged))
            *>Format the text as currency
                  set textBox1::Text to type 
                  String::Format(type System.Globalization.CultureInfo::CreateSpecificCulture("en-US"), "{0:C2}", ul)
                  invoke textBox1::add_TextChanged(new System.EventHandler(self::textBox1_TextChanged))
                  invoke textBox1::Select(textBox1::Text::Length, 0)
               end-if
               declare goodToGo as condition-value = self::TextisValid(textBox1::Text)
               if not goodToGo
                  set textBox1::Text to "$0.00"
                  invoke textBox1::Select(textBox1::Text::Length, 0)
               end-if
               goback.
        
           end method.
           method-id TextisValid private.
           procedure division using by value mytext as string
                              returning ret-bool as condition-value.
               declare money as type System.Text.RegularExpressions.Regex = 
                  new System.Text.RegularExpressions.Regex("^\$(\d{1,3}(\,\d{3})*|(\d ))(\.\d{2})?$")
               set ret-bool to money::IsMatch(mytext)
               goback.
           end method.
           
    
Children
No Data