代码如下:

private void txtXgongzi_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))
            {
                MessageBox.Show("只能输入数字","提示",MessageBoxButtons.OK ,MessageBoxIcon.Information );
                e.Handled=true;//已经处理此事件
            }
        }