Quantcast
Channel: Visual COBOL - Forum - Recent Threads
Viewing all articles
Browse latest Browse all 4356

RE: TextBox with currency format

$
0
0

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.
       

Viewing all articles
Browse latest Browse all 4356


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>