C# 文本框只能输入数字和退格键

参考:https://www.cnblogs.com/dtdnh520/articles/538684.html

按键不是数字和退格键,则忽略该键按下并释放的事件

        private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            KeyIsNotNumberOrBackspace(e);
        }
        
        /// 
        /// 按键不是数字和退格键
        /// 
        /// 
        /// 
        public static void KeyIsNotNumberOrBackspace(KeyPressEventArgs e)
        {
            bool keyIsNotNumberOrBackspace = e.KeyChar != '\b' && !Char.IsDigit(e.KeyChar);
            if (keyIsNotNumberOrBackspace)
            {
                e.Handled = true;
            }
        }

你可能感兴趣的:(C#,文本框数字)