在文本框中输入数字,动态的能判断有几位数字,如果输入第四个数字的时候能自动在文本框中显示千位分隔符,输入第七个数字的时候能自动显示第二个千位分隔符.类推.
希望能在文本框中动态显示... 谢谢

解决方案 »

  1.   

    帮输入的数值转换成字符串,然后用ToString("###,###") 的方式转化一下就可以了!
      

  2.   

    如何在textBox1_TextChanged 这样一个事件中去实现输入数字自动出现千位分隔符呢...
      

  3.   

    如果是整数的话,用toint32就行了
    textBox1.Text = Convert.ToDecimal(textBox1.Text).ToString("n2");
      

  4.   

    yuzhlhua 那个... 我一输入数字自动会出现小数点以及后面两位,如:5.00,然后光标会自动跳到数字的最前面
      

  5.   

    实在不好意思了,想不到了!
    如果不要求,可以在光标离开的时候在转化.
    tostring("n2")   n 表示有千位分隔符  2表示小数的位数.
      

  6.   

    private void textBox1_TextChanged(object sender, System.EventArgs e)
    {
    CultureInfo ci=(CultureInfo)Thread.CurrentThread.CurrentCulture.Clone();
    NumberFormatInfo nfi=ci.NumberFormat;
    nfi.CurrencyPositivePattern=1;
    nfi.CurrencyDecimalDigits=0;
    nfi.CurrencyGroupSeparator=",";
    nfi.CurrencySymbol="";
    ci.NumberFormat=nfi;
    Thread.CurrentThread.CurrentCulture=ci;
             //以上这些可不写到此事件中 如load里
    string str=textBox1.Text;
    textBox1.Text=Decimal.Parse(textBox1.Text.Trim()).ToString("c");
    textBox1.SelectionStart=textBox1.Text.Length;
    }
      

  7.   

    一种方法
    给TextBox添加2种状态,显示状态和编辑状态
    在显示状态下:TextBox的值表示为格式化值
    在编辑状态下:TextBox的值表示为正常数字值
    /////////////////////////////////////////////////////////////////
    public enum TextBoxMode {
    //显示模式
    Display, 
    //编辑模式
    Edit};public class TextBoxPlus : System.Windows.Forms.TextBox
    {
    #region 变量
    private double m_Value;
    private TextBoxMode m_Mode;
    #endregion #region 属性
    private string EditText
    {
    get
    {
    if (m_Mode != TextBoxMode.Edit)
    {
    throw new Exception("在非编辑模式下不能取得EditText值");
    }

    return this.Text;
    }
    set
    {
    if (m_Mode != TextBoxMode.Edit)
    {
    throw new Exception("在非编辑模式下不能设置EditText值");
    } this.TextChanged -= new EventHandler(TextChangedHandler); this.Text = value; this.TextChanged += new EventHandler(TextChangedHandler);
    }
    } private string DisplayText
    {
    get
    {
    if (m_Mode != TextBoxMode.Display)
    {
    throw new Exception("在非显示模式下不能取得DisplayText值");
    } return this.Text;
    }
    set
    {
    if (m_Mode != TextBoxMode.Display)
    {
    throw new Exception("在非显示模式下不能设置DisplayText值");
    }

    this.TextChanged -= new EventHandler(TextChangedHandler);
    this.Text = value;
    this.TextChanged += new EventHandler(TextChangedHandler);
    }
    } public  TextBoxMode Mode
    {
    get
    {
    return m_Mode;
    }
    set
    {
    if (m_Mode != value)
    {
    m_Mode = value;
    OnModeChanged();
    }
    }
    }
    #endregion #region 构造器
    public TextBoxPlus() : base()
    {
    m_Mode = TextBoxMode.Display;
    this.TextAlign = HorizontalAlignment.Right;
    this.TextChanged += new EventHandler(TextChangedHandler);
    }
    #endregion //TextChanged事件处理
    private void TextChangedHandler(object sender, System.EventArgs e)
    {
    if (m_Mode == TextBoxMode.Display)
    {
    SetDisplayTextToValue();
    }
    else
    {
    SetEditTextToValue();
    }
    } private void SetValueToDisplayText()
    {
    ////////////////////////////////////////////////
    //在这里加上数字格式化的代码
    //例:把数字格式化为保留小数点3位显示
    this.DisplayText = m_Value.ToString("0.000");
    /////////////////////////////////////////////////
    } private void SetValueToEditText()
    {
    this.EditText = m_Value.ToString();
    } private void SetEditTextToValue()
    {
    try
    {
    m_Value = double.Parse(this.EditText);
    }
    catch(Exception)
    {

    }
    } private void SetDisplayTextToValue()
    {
    throw new Exception("不能在显示值模式下修改Value值");
    } //模式变换事件处理
    private void OnModeChanged()
    {
    if (m_Mode == TextBoxMode.Display)
    {
    SetValueToDisplayText();
    }
    else
    {
    SetValueToEditText();
    }
    } //只能输入数字
    protected override void OnKeyPress(KeyPressEventArgs e)
    {
    base.OnKeyPress (e);

    switch(e.KeyChar)
    {
    case '1': return;
    case '2': return;
    case '3': return;
    case '4': return;
    case '5': return;
    case '6': return;
    case '7': return;
    case '8': return;
    case '9': return;
    case '0': return;
    case '.': 
    if (this.Text.IndexOf('.') != -1)
    {
    e.Handled = true;
    }
    return;
    case (char)Keys.Back : return;
    case (char)Keys.Left : return;
    case (char)Keys.Right : return;
    default : e.Handled = true;
    return;
    }
    } protected override void OnLeave(System.EventArgs e)
    {
    base.OnLeave(e);
    this.Mode = TextBoxMode.Display;
    } protected override void OnGotFocus(System.EventArgs e)
    {
    base.OnGotFocus(e);
    this.Mode = TextBoxMode.Edit;
    }
    }