Winforms 将文本框格式化为货币

2

本文介绍了Winforms 将文本框格式化为货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我是 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 将文本框格式化为货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

C# 中的多播委托奇怪行为?
Multicast delegate weird behavior in C#?(C# 中的多播委托奇怪行为?)...
2023-11-11 C#/.NET开发问题
6

参数计数与调用不匹配?
Parameter count mismatch with Invoke?(参数计数与调用不匹配?)...
2023-11-11 C#/.NET开发问题
26

如何将代表存储在列表中
How to store delegates in a List(如何将代表存储在列表中)...
2023-11-11 C#/.NET开发问题
6

代表如何工作(在后台)?
How delegates work (in the background)?(代表如何工作(在后台)?)...
2023-11-11 C#/.NET开发问题
5

没有 EndInvoke 的 C# 异步调用?
C# Asynchronous call without EndInvoke?(没有 EndInvoke 的 C# 异步调用?)...
2023-11-11 C#/.NET开发问题
2

Delegate.CreateDelegate() 和泛型:错误绑定到目标方法
Delegate.CreateDelegate() and generics: Error binding to target method(Delegate.CreateDelegate() 和泛型:错误绑定到目标方法)...
2023-11-11 C#/.NET开发问题
14