Winforms Format TextBox To Currency(Winforms 将文本框格式化为货币)
问题描述
我是 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 将文本框格式化为货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Winforms 将文本框格式化为货币


- 为什么 C# 中的堆栈大小正好是 1 MB? 2022-01-01
- 在 C# 中异步处理项目队列 2022-01-01
- 在 LINQ to SQL 中使用 contains() 2022-01-01
- CanBeNull和ReSharper-将其用于异步任务? 2022-01-01
- Azure Active Directory 与 MVC,客户端和资源标识同一 2022-01-01
- 是否可以在 .Net 3.5 中进行通用控件? 2022-01-01
- 使用 rss + c# 2022-01-01
- 带问号的 nvarchar 列结果 2022-01-01
- Windows 喜欢在 LINUX 中使用 MONO 进行服务开发? 2022-01-01
- C# 通过连接字符串检索正确的 DbConnection 对象 2022-01-01