【问题标题】:Textbox display formatting文本框显示格式
【发布时间】:2011-10-06 07:06:20
【问题描述】:

我想在每组 3 位数字后添加“,”。例如:当我输入 3000000 时,文本框将显示 3,000,000 但值仍然是 3000000。
我尝试使用 maskedtexbox,有一个缺点是 maskedtexbox 显示的数字类似于 _,__,__

【问题讨论】:

  • 使用 Validating 事件检查并重新格式化用户输入。

标签: c# winforms textbox formatting


【解决方案1】:

尝试将此代码添加到您的TextBoxKeyUp 事件处理程序

private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
    if (!string.IsNullOrEmpty(textBox1.Text))
    {
        System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
        int valueBefore = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands);
        textBox1.Text = String.Format(culture, "{0:N0}", valueBefore);
        textBox1.Select(textBox1.Text.Length, 0);
    }
}

是的,它会改变存储在 texbox 中的值,但是当您需要实际数字时,您可以使用以下行从文本中获取它:

int integerValue = Int32.Parse(textBox1.Text, System.Globalization.NumberStyles.AllowThousands);

当然不要忘记检查用户在文本框中输入的内容是否实际上是一个有效的整数。

【讨论】:

    【解决方案2】:

    我希望这可能适用于您的场景。

     private string text
            {
                get
                {
                    return text;
                }
                set
                {
                    try
                    {
                        string temp = string.Empty;
                        for (int i = 0; i < value.Length; i++)
                        {
                            int p = (int)value[i];
                            if (p >= 48 && p <= 57)
                            {
                                temp += value[i];
                            }
                        }
                        value = temp;
                        myTxt.Text = value;
                    }
                    catch
                    { 
    
                    }
                }
            }
    
        private void digitTextBox1_TextChanged(object sender, EventArgs e)
        {
            if (myTxt.Text == "")
                return;
            int n = myTxt.SelectionStart;
            decimal text = Convert.ToDecimal(myTxt.Text);
            myTxt.Text = String.Format("{0:#,###0}", text);
            myTxt.SelectionStart = n + 1;
        }
    

    这里,myTxt = 你的文本框。如下所示设置 Textchanged 事件,并像帖子中一样创建属性文本。

    希望对你有帮助。

    【讨论】:

      【解决方案3】:

      使用字符串格式

      int value = 300000
      String.Format("{0:#,###0}", value);
      // will return 300,000
      

      http://msdn.microsoft.com/en-us/library/system.string.format.aspx

      【讨论】:

        【解决方案4】:

        你可以像这样连接 OnKeyUp 事件:

         private void textBox1_KeyUp(object sender, KeyEventArgs e)
                {
                    if (!(e.KeyCode == Keys.Back))
                    {
                        string text = textBox1.Text.Replace(",", "");
                        if (text.Length % 3 == 0)
                        {
                            textBox1.Text += ",";
                            textBox1.SelectionStart = textBox1.Text.Length;
                        }
                    }
                }
        

        【讨论】:

          【解决方案5】:

          获取十进制值然后设置

          DecimalValue.ToString("#,#");
          

          【讨论】:

            猜你喜欢
            • 2018-10-16
            • 1970-01-01
            • 2012-10-04
            • 2017-02-10
            • 1970-01-01
            • 2014-12-26
            • 1970-01-01
            • 2012-09-11
            • 1970-01-01
            相关资源
            最近更新 更多