Currency style in a TextBox in WinForms

1

I need to put the text R $ in textbox so that it is in the following format:

  

R $: 1,200,58

I'm trying the following unsuccessful ways:

textbox1.Text = Convert.ToDouble(textbox1.Text).ToString("C"); 

and

textbox1.Text = Decimal.Parse(texbox.text).ToString("N2");
    
asked by anonymous 07.08.2014 / 14:05

4 answers

1

Then @Fricio is very simple.

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("pt-BR");
        label.Text = string.Format("{0:C}", Convert.ToDouble(1200.58));

But remember everything will depend on your CultureInfo and CultureUi . If they are in another language, it will not work the way you want!

    
07.08.2014 / 21:41
3

If you accept the use of events, you can use this way:

Masking function

    private void RetornarMascara(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)sender;
        txt.Text = double.Parse(txt.Text).ToString("C2");
    }

Masking feature

    private void TirarMascara(object sender, EventArgs e)
    {
        TextBox txt = (TextBox)sender;
        txt.Text = txt.Text.Replace("R$", "").Trim();
    }

Function to only allow numbers and commas

    private void ApenasValorNumerico(object sender, KeyPressEventArgs e)
    {
        TextBox txt = (TextBox)sender;
        if (!char.IsDigit(e.KeyChar) && e.KeyChar != Convert.ToChar(Keys.Back))
        {
            if (e.KeyChar == ',')
            {
                e.Handled = (txt.Text.Contains(','));
            }
            else
                e.Handled = true;
        }
    }

Applies all functions to their events

    private void AplicarEventos(TextBox txt)
    {
        txt.Enter += TirarMascara;
        txt.Leave += RetornarMascara;
        txt.KeyPress += ApenasValorNumerico;
    }

Example usage:

    public Form1()
    {
        InitializeComponent();

        AplicarEventos(textBox1);
        AplicarEventos(textBox2);
    }
    
07.08.2014 / 20:14
2

I use the javascript function below to put the mask:

function MascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e) {
var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
if (!e) e = window.event;
var whichCode = e.which || e.keyCode;
if (isValidKey(whichCode)) return true;
key = String.fromCharCode(whichCode); // Valor para o c¢digo da Chave
if (strCheck.indexOf(key) == -1) return false; // Chave inv lida
len = objTextBox.value.length;
for (i = 0; i < len; i++)
    if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
aux = '';
for (; i < len; i++)
    if (strCheck.indexOf(objTextBox.value.charAt(i)) != -1) aux += objTextBox.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) objTextBox.value = '';
if (len == 1) objTextBox.value = '0' + SeparadorDecimal + '0' + aux;
if (len == 2) objTextBox.value = '0' + SeparadorDecimal + aux;
if (len > 2) 
{
    aux2 = '';
    for (j = 0, i = len - 3; i >= 0; i--) 
    {
        if (j == 3) 
        {
            aux2 += SeparadorMilesimo;
            j = 0;
        }
        aux2 += aux.charAt(i);
        j++;
    }
    objTextBox.value = '';
    len2 = aux2.length;
    for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
    objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
}
return false;
}

Place the call in the onkeypress event of your textbox, return (this, '.', ',', event));

Test and see if it causes the desired effect.

Or you can use some jquery library, for example: link

    
07.08.2014 / 17:02
0

Create a textbox with the name txt_valor and assign the events KeyPress , Leave , KeyUp , and a value string

string valor;
private void txt_valor_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!Char.IsDigit(e.KeyChar) && e.KeyChar != Convert.ToChar(Keys.Back))
    {
        if (e.KeyChar == ',')
        {
            e.Handled = (txt_valor.Text.Contains(","));
        }
        else
            e.Handled = true;
    }            
}

private void txt_valor_Leave(object sender, EventArgs e)
{
    valor = txt_valor.Text.Replace("R$", "");
    txt_valor.Text = string.Format("{0:C}", Convert.ToDouble(valor));
}

private void txt_valor_KeyUp(object sender, KeyEventArgs e)
{
    valor = txt_valor.Text.Replace("R$","").Replace(",","").Replace(" ","").Replace("00,","");
    if(valor.Length == 0)
    {
        txt_valor.Text = "0,00"+valor;
    }
    if(valor.Length == 1)
    {
        txt_valor.Text = "0,0"+valor;
    }
    if(valor.Length == 2)
    {
        txt_valor.Text = "0,"+valor;
    }
    else if(valor.Length >= 3)
    {
        if(txt_valor.Text.StartsWith("0,"))
        {
            txt_valor.Text = valor.Insert(valor.Length - 2,",").Replace("0,","");
        }
        else if(txt_valor.Text.Contains("00,"))
        {
            txt_valor.Text = valor.Insert(valor.Length - 2,",").Replace("00,","");
        }
        else
        {
            txt_valor.Text = valor.Insert(valor.Length - 2,",");
        }
    }           
    valor = txt_valor.Text;
    txt_valor.Text = string.Format("{0:C}", Convert.ToDouble(valor));
    txt_valor.Select(txt_valor.Text.Length,0);
}
    
17.04.2018 / 19:04