我正在使用C#为Windows应用程序创建CurrencyTextBox
。它几乎已经完成,但是当CurrencyTextBox
显示其文本时,我遇到了问题。它不是货币格式化的。例如:
20000应显示为$ 20,000.00
为了显示为$ 20,000.00,我必须调用格式代码。
private void Form1_Load(object sender, EventArgs e)
{
currencyTextBox1.Value = 20000;
currencyTextBox1.Text = currencyTextBox1.Value.ToString("C");
}
有没有其他方法来简化这个问题?
以下是CurrencyTextBox的代码:
public class CurrencyTextBox : TextBox
{
private decimal _value = 0;
public CurrencyTextBox()
{
base.TextAlign = HorizontalAlignment.Right;
}
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
this.Text = _value.ToString();
if (this.Text == "0")
this.Clear();
this.SelectionStart = this.Text.Length;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
NumberFormatInfo numberFormatInfo = CultureInfo.CurrentCulture.NumberFormat;
string decimalSeparator = numberFormatInfo.NumberDecimalSeparator;
string groupSeparator = numberFormatInfo.NumberGroupSeparator;
string negativeSign = numberFormatInfo.NegativeSign;
// Workaround for groupSeparator equal to non-breaking space
if (groupSeparator == ((char)160).ToString())
{
groupSeparator = " ";
}
// Allows only numbers, decimals and control characters
if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && e.KeyChar != decimalSeparator[0])
{
e.Handled = true;
}
if (e.KeyChar == decimalSeparator[0] && this.Text.Contains(decimalSeparator[0]))
{
e.Handled = true;
}
if (e.KeyChar == decimalSeparator[0] && this.Text.Length < 1)
{
e.Handled = true;
}
}
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
try
{
Value = Convert.ToDecimal(this.Text);
}
catch { }
}
protected override void OnValidated(EventArgs e)
{
base.OnValidated(e);
try
{
// format the value as currency
decimal dTmp = Convert.ToDecimal(this.Text);
this.Text = dTmp.ToString("C");
}
catch { }
}
public decimal Value
{
get { return this._value; }
set { this._value = value; }
}
}
答案 0 :(得分:2)
保留所有原始代码,我改变了两件事,一切似乎都很好用:
protected override void OnTextChanged(EventArgs e)
{
base.OnTextChanged(e);
try
{
this._value = Convert.ToDecimal(this.Text); // Assign private field instead of property, due to the next change.
}
catch { }
}
public decimal Value
{
get { return this._value; }
set
{
this._value = value;
this.Text = this._value.ToString("C"); // Set the text when Value is set.
}
}
然后您的使用就像:
private void Form1_Load(object sender, EventArgs e)
{
currencyTextBox1.Value = 20000;
}
答案 1 :(得分:0)
您应该使用textChanged
事件,因此在分配值时会应用格式。
public Form1()
{
InitializeComponent();
textBox1.Text = "20000";
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
FormatText();
}
private void FormatText()
{
if (String.IsNullOrWhiteSpace(textBox1.Text)) // Validate input
return;
decimal amount;
Decimal.TryParse(textBox1.Text, out amount);
if (amount == 0) // Validate if it's a number
return;
textBox1.Text = "$" + String.Format("{0:n0}",amount); // Format with no decimals
textBox1.Select(textBox1.Text.Length, 0); // Set the cursor position to last character
}
答案 2 :(得分:0)
简单TextBox
没有可以设置货币格式的属性。但是,有一些方法可用于将TextBox文本格式化为正确的格式。
1)创建自定义控件并继承TextBox,您可以在其中覆盖TextBox
的{{1}}属性并进行更改。
Text
2)如果您不想创建自定义控件,则可以在表单中定义属性并使用该属性而不是TextBox的Text属性。
public shadow string Text
{
get{
return base.Text.Replace("$", "").Replace(",","");
}
set{
base.Text = Convert.ToDecimal(value).ToString("C");
}
}
注意:这只是为了创造想法。您需要修改代码以返回并正确设置值。