Winforms 将文本框格式化为货币

发布于 2024-12-02 02:22:41 字数 283 浏览 0 评论 0原文

我是 Winforms 开发的新手,我将在文本框中向用户显示数据。文本框将与货币数据进行数据绑定,因此我尝试格式化正在显示的值。

我查看了一个蒙版文本框,但这并不是我正在寻找的,因为它没有将分放在小数点后。

我需要为每个文本框编写与此类似的代码吗?

TextBox.Text = DataSet.DataView[0].Amount.ToString("c");

我有很多文本框需要格式化,所以我想知道是否需要为每个文本框执行此操作。有人有什么建议吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-12-09 02:22:41

您可以创建自己的从标准文本框派生的文本框

 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;

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);
        }
    }
}

Usage:

   textbox.Format = "c";
   textbox.Datasource = DataSet.DataView[0].Amount;
夏末 2024-12-09 02:22:41

假设您有来自双变量(例如 mySumInvestment)的总计,并且您希望将其放置在美国货币格式的文本框中。那么这就是你可以做的事情

textBox5.Text = mySumInvestment.ToString("c", CultureInfo.CreateSpecificCulture("en-US")); // In order to format as currency

Imagine you have the total coming from a double variable such as mySumInvestment and you want to place it a textbox with US Currency Format. Then this is something you could do

textBox5.Text = mySumInvestment.ToString("c", CultureInfo.CreateSpecificCulture("en-US")); // In order to format as currency
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文