Highlighted
Absent Member.
Absent Member.
1674 views

Aply currency formating in textbox of datagridview

Jump to solution

I maded one post about currency formating, and the question was answered, but I am now trying to put the system formatting within a field in a datagridview, so, I C# for me is more easy to make that, and I maded, but, When I tryed to make the same in Visual Cobol, I just got errors 😞

My C# code is that, remambar, is based in that code: http://community.microfocus.com/microfocus/cobol/visual_cobol/f/18/t/10425.aspx

private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
e.Control.PreviewKeyDown -= Control_PreviewKeyDown;
e.Control.PreviewKeyDown += new PreviewKeyDownEventHandler(Control_PreviewKeyDown);
}

private void Control_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
var txtB = (TextBox)sender;

var myValue = txtB.Text.Replace(",", "").Replace("R$", "").Replace(".", "").TrimStart('0');
decimal ul;

if (Decimal.TryParse(myValue, out ul))
{
ul = ul / 100;
txtB.PreviewKeyDown -= new PreviewKeyDownEventHandler(Control_PreviewKeyDown);
txtB.Text = string.Format(System.Globalization.CultureInfo.CreateSpecificCulture("pt-BR"), "{0:C2}", ul);
txtB.PreviewKeyDown += new PreviewKeyDownEventHandler(Control_PreviewKeyDown);
txtB.Select(txtB.Text.Length, 0);
}

if (!TextIsValid(txtB.Text))
{
txtB.Text = "R$ 0,00";
txtB.Select(txtB.Text.Length, 0);
}
}

private bool TextIsValid(string myText)
{
var mn = new System.Text.RegularExpressions.Regex(@"^R\$ ?([1-9]{1}[\d]{0,2}(.[\d]{3})*(,[\d]{0,2})?|[1-9]{1}Drinks{0,}(,[\d]{0,2})?|0(,[\d]{0,2})?|(,[\d]{1,2})?)$");
if (mn.IsMatch(myText))
return true;
else
return false;
}

But, the strange thing is, in that code, in C#, the formating of text not work so fine than in the textbox, why ?

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Aply currency formating in textbox of datagridview

Jump to solution

I got this to work by modifying the previous example to use a datagridview instead of a textbox control but to get the textbox control out of the current cell and use the same methods on it.

       class-id entercurrency.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       
       working-storage section.
       01 txtBox type TextBox.
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent
           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.
       

       method-id dataGridView1_EditingControlShowing final private.
       procedure division using by value sender as object e as type System.Windows.Forms.DataGridViewEditingControlShowingEventArgs.
           
           if DataGridView1::CurrentCellAddress::X = 1  
              set txtBox to e::Control as type TextBox
              if txtBox not = null
                 invoke txtBox::remove_TextChanged(new System.EventHandler(self::txtBox_TextChanged))
                 invoke txtBox::add_TextChanged(new System.EventHandler(self::txtBox_TextChanged))
              end-if
          end-if
       end method.

       method-id txtBox_TextChanged public.
       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 = 
           txtBox::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 txtBox::remove_TextChanged(new System.EventHandler(self::txtBox_TextChanged))
                            
        *>Format the text as currency
              set txtBox::Text to type 
              String::Format(type System.Globalization.CultureInfo::CreateSpecificCulture("en-US"), "{0:C2}", ul)
              invoke txtBox::add_TextChanged(new System.EventHandler(self::txtBox_TextChanged))
              invoke txtBox::Select(txtBox::Text::Length, 0)
           end-if
           declare goodToGo as condition-value = self::TextisValid(txtBox::Text)
           if not goodToGo
              set textBox1::Text to "$0.00"
              invoke txtBox::Select(txtBox::Text::Length, 0)
           end-if
           goback.
    
       end method.
       
       end class.

View solution in original post

0 Likes
1 Reply
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Aply currency formating in textbox of datagridview

Jump to solution

I got this to work by modifying the previous example to use a datagridview instead of a textbox control but to get the textbox control out of the current cell and use the same methods on it.

       class-id entercurrency.Form1 is partial
                 inherits type System.Windows.Forms.Form.
       
       working-storage section.
       01 txtBox type TextBox.
       method-id NEW.
       procedure division.
           invoke self::InitializeComponent
           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.
       

       method-id dataGridView1_EditingControlShowing final private.
       procedure division using by value sender as object e as type System.Windows.Forms.DataGridViewEditingControlShowingEventArgs.
           
           if DataGridView1::CurrentCellAddress::X = 1  
              set txtBox to e::Control as type TextBox
              if txtBox not = null
                 invoke txtBox::remove_TextChanged(new System.EventHandler(self::txtBox_TextChanged))
                 invoke txtBox::add_TextChanged(new System.EventHandler(self::txtBox_TextChanged))
              end-if
          end-if
       end method.

       method-id txtBox_TextChanged public.
       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 = 
           txtBox::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 txtBox::remove_TextChanged(new System.EventHandler(self::txtBox_TextChanged))
                            
        *>Format the text as currency
              set txtBox::Text to type 
              String::Format(type System.Globalization.CultureInfo::CreateSpecificCulture("en-US"), "{0:C2}", ul)
              invoke txtBox::add_TextChanged(new System.EventHandler(self::txtBox_TextChanged))
              invoke txtBox::Select(txtBox::Text::Length, 0)
           end-if
           declare goodToGo as condition-value = self::TextisValid(txtBox::Text)
           if not goodToGo
              set textBox1::Text to "$0.00"
              invoke txtBox::Select(txtBox::Text::Length, 0)
           end-if
           goback.
    
       end method.
       
       end class.

View solution in original post

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.