I am trying to ensure that the text in my control derived from TextBox is always formatted as currency.
I have overridden the Text property like this.
public override string Text
{
get
{
return base.Text;
}
set
{
double tempDollarAmount = 0;
string tempVal = value.Replace("$", "").Replace(",","");
if (double.TryParse(tempVal, out tempDollarAmount))
{
base.Text = string.Format("C", tempDollarAmount);
}
else
{
base.Text = "$0.00";
}
}
}
Results:
I assume that I am missing something very simple here. Is it something about the property? Or am I using the string format method incorrectly?
Instead of "C" put "{0:c}"
For more string formatting problems go here
You can also use tempDollarAmount.ToString("C").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With