问题描述
我是 Winforms 开发的新手,我将在文本框中向我的用户显示数据.文本框将与货币数据绑定,因此我尝试格式化正在显示的值.
I am new to Winforms development and I going to be displaying data to my users in a textbox. The textbox will be databound with data that is currency so I am trying to Format the value that is being displayed.
我查看了一个蒙面文本框,但这并不是我想要的,因为它没有将美分放在小数点后.
I looked at a Masked Text Box but that isn't exactly what I am looking for because it doesn't put the cents after the decimal.
我需要为每个与此类似的文本框编码吗?
Do I need to code for each textbox similar to this?
TextBox.Text = DataSet.DataView[0].Amount.ToString("c");
我有很多需要格式化的文本框,所以我想知道是否需要为每个文本框都这样做.有人有什么建议吗?
I have alot of textboxes that need to be formatted so I am wondering if I need to do this for each one. Does anyone have any suggestions?
推荐答案
您可以创建自己的 TextBox 派生自标准文本框
You can create your own TextBox derived from standard one
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;
这篇关于Winforms 将文本框格式化为货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!