Winforms 将文本框格式化为货币

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)