WinForm 限制TextBox只能输入数字和小数点

 /// <summary>
        /// 限制TextBox只能输入数字和小数点
        /// 一般在TextBox的KeyPress中调用
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(((e.KeyChar >= '0') && (e.KeyChar <= '9')) || e.KeyChar <= 31))
            {
                if (e.KeyChar == '.')
                {
                    if (((TextBox)sender).Text.Trim().IndexOf('.') > -1)
                        e.Handled = true;
                }
                else
                    e.Handled = true;
            }
            else
            {
                if (e.KeyChar <= 31)
                {
                    e.Handled = false;
                }
            }
        }
posted @ 2011-03-22 15:31  我在深圳搬砖  阅读(1777)  评论(6编辑  收藏  举报