我试过用MaskedTextBox控件,将期Mask属性设为"990.999",感觉效果不是很好。
因为当我想输入"1.234"时,会得先输入两个空格,才能输进去,要不然就成了123.4。
另外,如果用TextBox控件,然后用Regex进行验证,感觉也不是很方便,因为只有在输入完成后才可验证。
大家有没有什么好的建议??
我的代码如下, 数字保留三位小数.
  private void tbRate_Validating(object sender, CancelEventArgs e)
        {
            tbRate.Text = double.Parse(tbRate.Text).ToString();
            int pos = tbRate.Text.LastIndexOf('.');
            if (pos < 0)
            {
                tbRate.Text += ".000";
            }
            else
            {
                tbRate.Text = tbRate.Text.PadRight(tbRate.Text.Length + (3 - (tbRate.Text.Length - pos - 1), '0');
            }
}
        private void tbRate_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar < '0' || e.KeyChar > '9')
            {
                e.Handled = true;
            }
            if (e.KeyChar == '\b')    // '\b'表示退格鍵
            {
                e.Handled = false;
            }
            if (e.KeyChar == '.')
            {
                if (tbRate.Text.LastIndexOf('.') >= 0)  // 只允許輸入一個小數點號
                {
                    e.Handled = true;
                }
                else
                {
                    e.Handled = false;
                }
            }
        }
[此贴子已经被作者于2007-5-9 15:25:44编辑过]
