TextBox实现仅允许输入数字

Posted on 2019-11-23 20:26  冰糖Luck1996  阅读(102)  评论(0编辑  收藏  举报

使用TextBox的KeyPress事件

        /// <summary>
        /// 去除非十进制和Backspace和Enter
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar!=8 && !char.IsDigit(e.KeyChar))&&e.KeyChar!=13)
            {
                MessageBox.Show("请输入数字","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Information);
                e.Handled = true;
            }
        }
textBox1_KeyPress