Answers for "C# convert random numbers in textBox to currency"

C#
0

C# convert random numbers in textBox to currency

private void textBox1_Validating(object sender, CancelEventArgs e)
{
    string value;
    NumberStyles style;
    CultureInfo culture;
    decimal currency;

    value = textBox1.Text;
    style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
    culture = CultureInfo.CreateSpecificCulture("en-US");
    if (!Decimal.TryParse(value, style, culture, out currency))
    {
        MessageBox.Show("Please enter a valid currency amount.", "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Error);
        // prevent the textbox from losing focus
        e.Cancel = true;
    } 
}

private void textBox1_Validated(object sender, EventArgs e)
{
    string input = textBox1.Text.Trim();
    if (input.StartsWith("$"))
    {
        string temp = input.Replace("$","");
        string specifier = "C";
        CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US");
        textBox1.Text = Decimal.Parse(temp).ToString(specifier, culture);
    }
}
Posted by: Guest on October-20-2020
0

C# convert random numbers in textBox to currency

public void textchange(object sender, EventArgs e)
{
   if(textbox1.Text.Contains("$"))
   {
     string valueToConvert = textbox1.Text.replace("$","");
     double newVal = Convert.ToDouble(valueToConvert);

     //Assign the new value with $ to textbox1
     textbox1.Text = "$"+Convert.ToString(newVal);
   }
}
Posted by: Guest on October-20-2020
0

C# convert random numbers in textBox to currency

// Com esse método o R$ fica fixo e a vírgula se move de acordo com as casas decimais
//
//Crie um textbox com o name txt_valor e adicione os eventos KeyPress, KeyUp, Leave e //uma string valor

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);
        }
Posted by: Guest on October-20-2020
0

C# convert random numbers in textBox to currency

int input = 40;
string output = $"{input:C2}";
Posted by: Guest on October-20-2020

Code answers related to "C# convert random numbers in textBox to currency"

C# Answers by Framework

Browse Popular Code Answers by Language