WinForms格式文本框到货币 [英] Winforms Format TextBox To Currency

查看:170
本文介绍了WinForms格式文本框到货币的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Winforms开发的新手,我将在文本框中向用户显示数据。该文本框将数据绑定与货币数据,所以我想格式化正在显示的值。



我看了一个Masked Text Box,但是这不是我正在寻找的,因为它不会把小数点后面的分。 b
$ b

我是否需要为每个文本框编写相似的代码?

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

我有很多的文本框需要格式化,所以我想知道如果我需要这样做每一个。有没有人有任何建议?

解决方案

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

  public class TextBoxEx:TextBox 
{
public string Format {get;组; }

private object datasource = new object();
公共对象数据源
{
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);





$ b用法:

  textbox.Format =c; 
textbox.Datasource = DataSet.DataView [0] .Amount;


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?

解决方案

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;

这篇关于WinForms格式文本框到货币的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆