这可能是一个模糊的问题,但我试图找到一个函数,将表单上的所有文本框设置为货币格式。在我的表格上,我有一些预设的盒子,但只需按一下按钮就会有很多动态。这些文本框中的信息将来自Access。我有一个用于清除文本框的示例功能,我很好奇是否有类似于我要求的内容。
private void txtMaxDiscount_TextChanged(object sender, EventArgs e)
{
double amount = 0.0d;
if (double.TryParse(txtMaxDiscount.Text, NumberStyles.Currency, null, out amount))
{
txtMaxDiscount.Text = amount.ToString("C");
}
}
答案 0 :(得分:0)
您可以按照此stack overflow post创建自己的控件。您可以创建自己的文本框,从基础控件派生。因此,您可以指定自己的数据源和格式,但也可以使用其他功能扩展控件。同样,这将允许重复使用,而不会重复。
public class TextBoxEx : TextBox
{
public string Format { get; set; }
private object datasource = new object();
public object Datasource
{
get { return datasource; }
set
{
datasource = value;
if (datasource == null)
base.Text = string.Empty;
else if(string.IsNullOrWhiteSpace(Format))
base.Text = datasource.ToString();
else
base.Text = string.Format("{0:"+ Format + "}",datasource);
}
}
}
用法:
textbox.Format = "c";
textbox.Datasource = DataSet.DataView[0].Amount;
答案 1 :(得分:0)
我想我理解你的要求。您希望自动格式化表单上的所有文本框,包括动态添加的新文本框。
要实现这一点,第一步是在任何货币文本框丢失焦点时设置通用事件处理程序:
void OnCurrencyTextBoxLeave(object sender, EventArgs e)
{
if (sender is TextBox)
{
decimal value;
var textBox = (TextBox)sender;
if (decimal.TryParse(textBox.Text, out value))
{
textBox.Text = value.ToString("C");
}
else textBox.Text = string.Empty;
}
}
然后我们可以在表单中添加两个函数,这些函数会将文本框订阅到此事件处理程序以自动处理格式。第一个在创建表单时只调用一次,并处理在设计时(或首次创建表单时)添加的任何文本框:
private void SubscribeCurrencyTextboxes(Control container)
{
foreach (Control control in container.Controls)
{
if (!(control is TextBox)) continue;
((TextBox)control).Leave += OnCurrencyTextBoxLeave;
}
}
要使用此功能,我们只需在表单加载事件中调用它,或覆盖OnLoad
,如下所示:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
SubscribeCurrencyTextboxes(this);
}
我们应该再添加一个函数来在运行时订阅动态创建的文本框:
private void SubscribeCurrencyTextboxes(params TextBox[] textBoxes)
{
foreach (var textBox in textBoxes) textBox.Leave += OnCurrencyTextBoxLeave;
}
现在,当您动态创建文本框时,请向SubscribeCurrencyTextboxes
添加另一个调用,并为其提供实例列表,例如:
SubscribeCurrencyTextboxes(textBox1, textBox2, ... textBox20);
当用户离开表单上的每个文本框时,它将自动格式化为货币!
实现相同结果的另一种方式(也许是更好的方法) - 特别是如果你需要重新使用这种行为 - 将继承TextBox
并扩展它。 (请在此处查看其他答案,提出相同的建议。)我们可以调用继承的控件CurrencyTextBox
,而不是动态地将普通文本框添加到表单中,您可以添加此自定义类型的文本框。
为了帮助您入门,您可以声明:
public class CurrencyTextBox : TextBox
{
// Apply currency formatting when text is set from code:
public override string Text
{
get { return base.Text; }
set
{
base.Text = Format(RemoveCurrencyFormatting(value));
}
}
// Expose decimal currency value to code:
public decimal Value
{
get { return this.value; }
set { this.value = value; }
}
private decimal value = 0m;
// Remove currency formatting dollar signs and commas on focus:
protected override void OnEnter(EventArgs e)
{
base.OnEnter(e);
this.Text = RemoveCurrencyFormatting(this.Text);
this.SelectAll();
}
// Apply currency formatting when focus is lost:
protected override void OnLeave(EventArgs e)
{
base.OnLeave(e);
Format(this.Text);
}
// Perform actual formatting:
private string Format(string text)
{
if (!decimal.TryParse(text, out this.value))
{
return value.ToString("C");
}
else return string.Empty;
}
// Remove currency formatting dollar signs and commas:
private string RemoveCurrencyFormatting(string value)
{
if (!string.IsNullOrEmpty(value))
{
return value
.Replace(NumberFormatInfo.CurrentInfo.CurrencySymbol, string.Empty)
.Replace(NumberFormatInfo.CurrentInfo.NumberGroupSeparator, string.Empty);
}
else return string.Empty;
}
}
此示例使用NumberFormatInfo
来处理货币符号(例如北美的美元符号和千位分隔符逗号),这可能会使任何类型的货币格式化。请务必向 System.Globalization 添加using
指令:
using System.Globalization;
您可以在this MSDN page上找到有关NumberFormatInfo
的更多信息。