【问题标题】:maskedTextBox in windows form applicationwindows窗体应用程序中的maskedTextBox
【发布时间】:2014-11-09 09:25:19
【问题描述】:

在 windows 窗体应用程序中,C# 使用 Visual Studio 2012

我的掩码是 0.00,但是当我输入 3.00 时,它只考虑 3 而不是 3.00 并且当输入 3.30 它考虑 3.3 但如果我输入 3.01 则考虑这个值。

问题是我希望它也考虑最后一个零。

在表单设计器代码中:

this.maskedTextBox1.Location = new System.Drawing.Point(402, 121);
this.maskedTextBox1.Mask = "0.00";
this.maskedTextBox1.Name = "maskedTextBox1";
this.maskedTextBox1.PromptChar = '-';
this.maskedTextBox1.Size = new System.Drawing.Size(31, 22);
this.maskedTextBox1.TabIndex = 23;
this.maskedTextBox1.ValidatingType = typeof(System.DateTime);
this.maskedTextBox1.MaskInputRejected += new 

System.Windows.Forms.MaskInputRejectedEventHandler(this.maskedTextBox1_MaskInputRejected);

【问题讨论】:

  • 显示maskedTextBox1_MaskInputRejected内的代码
  • maskedTextBox1_MaskInputRejected 为空.. 因为 0 默认只允许 0 到 9 位数字作为微软的网站msdn.microsoft.com/en-us/library/…
  • 您能解释一下为什么将 DateTime 用作 ValidatingType 而不是小数(或双精度...)
  • @steve 实际上我想验证双精度...becz 用户在此字段中输入他们的 gpa/标记.. 包括小数点,如 3.44 3.00 等,但仅限于 0-9 位数字..this工作正常,但不适用于我在上面的查询中提到的特殊情况。

标签: winforms mask maskedtextbox


【解决方案1】:

Mask 属性适用于 Maskedit 中显示的格式以及可以输入的字符。它不会自动为您设置文本格式。

如果要显示所需格式的字符串,则需要将用户键入的文本转换为有效的小数,然后使用 composite formatting 准备一个字符串,以根据需要显示双精度

double d = Convert.ToDouble(maskedTextBox1.Text)
Console.WriteLine(string.Format("{0:N2}", d));

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-21
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多