简体   繁体   English

C#winforms texbox中的千位分隔符

[英]thousand separator in C# winforms texbox

i want to put thousand separator in my textbox. 我想在文本框中输入千位分隔符。 i have written following code but it does not work well. 我已经编写了以下代码,但效果不佳。 for example : 例如 :

1- i cannot type 30000. 1-我无法输入30000。

2- 123,456 => 561,234. 2-123,456 => 561,234。

what is the problem? 问题是什么?

private void TextBoxCostTextChanged(object sender, EventArgs e)
{
    try
    {
        var context = this.TextBoxCost.Text;
        bool ischar = true;
        for (int i = 0; i < context.Length; i++)
        {
            if (char.IsNumber(context[i]))
            {
                ischar = false;
                break;
            }
        }
        if (ischar)
        {
            TextBoxCost.Text = null;                         
        }

        **TextBoxCost.Text = string.Format("{0:#,###}", double.Parse(TextBoxCost.Text));**

    }
    catch (Exception ex)
    {
        ExceptionkeeperBll.LogFileWrite(ex);
    }
}

i solved my problem: 我解决了我的问题:

 private void TextBoxCostKeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (!char.IsControl(e.KeyChar)
        && !char.IsDigit(e.KeyChar)
        && e.KeyChar != '.')
                {
                    e.Handled = true;
                }


            }
            catch (Exception ex)
            {
                ExceptionkeeperBll.LogFileWrite(ex);
            }
        }

        private void TextBoxCostTextChanged(object sender, EventArgs e)
        {
            try
            {
                  string value = TextBoxCost.Text.Replace(",", "");
      ulong ul;
      if (ulong.TryParse(value, out ul))
      {
          TextBoxCost.TextChanged -= TextBoxCostTextChanged;
          TextBoxCost.Text = string.Format("{0:#,#}", ul);
          TextBoxCost.SelectionStart = TextBoxCost.Text.Length;
          TextBoxCost.TextChanged += TextBoxCostTextChanged;
      }
            }
            catch (Exception ex)
            {
                ExceptionkeeperBll.LogFileWrite(ex);
            }
        }

First, you can have an easier way to check that all characters in your text box are numbers , not letters . 首先,您可以使用一种更简单的方法来检查文本框中的所有字符是否都是numbers ,而不是letters

double inputNumber;
bool isNumber = double.TryParse(TextBoxCost.Text, out inputNumber);

Second, you are using the wrong function . 其次,您使用了错误的函数 String.Format is used for inserting values into a string. String.Format用于将值插入字符串。 ToString() can be used to transform the display format of a string (odd terminology, but yeah). ToString()可用于转换字符串的显示格式(奇数术语,但可以)。

Use the following to get the number with commas 使用以下命令获取带逗号的数字

string withCommas = inputNumber.ToString("#,##0");
TextBoxCost.Text = withCommas;

Notice I am NOT using String.Format . 注意,我没有使用String.Format Stop using String.Format 停止使用String.Format

TextBox es tend to set the cursor position to the beginning when the text is changed in the background. TextBox倾向于在后台更改文本时将光标位置设置为开始。 So, for the input to be as intuitive as possible, you need to do the following: 因此,为了使输入尽可能直观,您需要执行以下操作:

  1. Split the string at the current cursor position and remove all foreign characters (including thousands separators, because you want to insert them afterwards) from the first part 在当前光标位置分割字符串,并从第一部分中删除所有外来字符(包括数千个分隔符,因为您要在以后插入它们)
  2. Get the correctly formatted value (and maybe the numerical value of the input, if your application cares for that) 获取正确格式的值(如果您的应用程序需要,还可以输入的数字值)
  3. Get the position of the last character of the first part of the input string (see #1) in the formatted string (by comparing them character-wise, skipping the thousands separators) 获取格式字符串中输入字符串第一部分的最后一个字符的位置(请参阅#1)(通过按字符进行比较,跳过数千个分隔符)
  4. Update the value of your TextBox 更新您的TextBox的值
  5. Set the cursor position to the position calculated at #3 将光标位置设置为在#3处计算出的位置

The only problem with the above algorithm will be that when entering a thousands separator at the right position, the cursor will end up directly before it, but solving that (and actually writing the code) is left as an exercise ;) 上面算法的唯一问题是,当在正确的位置输入一个千位分隔符时,光标将直接在其前面结束,但是解决该问题(实际上是编写代码)仍然是一个练习;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM