C# WinC中的千位分隔符

C# WinC中的千位分隔符,c#,string,string-formatting,c#-5.0,C#,String,String Formatting,C# 5.0,我想在我的文本框中输入千位分隔符。我已经写了下面的代码,但它不能很好地工作。例如: 1-我不能输入30000 2-123456=>561234 有什么问题 private void TextBoxCostTextChanged(object sender, EventArgs e) { try { var context = this.TextBoxCost.Text; bool ischar = true; for (int i

我想在我的文本框中输入千位分隔符。我已经写了下面的代码,但它不能很好地工作。例如:

1-我不能输入30000

2-123456=>561234

有什么问题

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);
    }
}

首先,您可以用一种更简单的方法检查文本框中的所有字符是否都是数字,而不是字母

第二,您使用了错误的函数。格式用于将值插入字符串中。ToString可以用来转换字符串的显示格式,但是是的

使用以下命令获取带有逗号的数字

string withCommas = inputNumber.ToString("#,##0");
TextBoxCost.Text = withCommas;
请注意,我没有使用String.Format。停止使用字符串。当文本在后台更改时,格式文本框倾向于将光标位置设置为开头。因此,为了使输入尽可能直观,您需要执行以下操作:

在当前光标位置拆分字符串并删除所有外来字符,包括数千个分隔符,因为您希望在第一部分之后插入它们 如果您的应用程序需要,请获取正确格式的值以及输入的数值 通过按字符进行比较,跳过数千个分隔符,获取输入字符串第一部分的最后一个字符(请参见1)在格式化字符串中的位置 更新文本框的值 将光标位置设置为在3处计算的位置 上述算法的唯一问题是,当在正确的位置输入千位分隔符时,光标将直接在它前面结束,但解决这个问题并实际编写代码只是一个练习

我解决了我的问题:

 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);
            }
        }

相当肯定,*321英镑打破了你的answer@Sayse,改为“全部”并为数字他,很抱歉太迂腐了:+1但不确定小数点@gunr2171,TextBoxCost.Text=string.Format{0:,0},double.ParseTextBoxCost.Text;没有work@sami,请参阅我的最新答案。你使用了错误的方法。
 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);
            }
        }