textbox只能输入数字

txtLocPort.KeyPress += Pub.KeyPress_NumInput;

 /// <summary>
        ///  数字输入 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public static void KeyPress_NumInput(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar < '0' || e.KeyChar > '9') && e.KeyChar != 8)
                e.KeyChar = (char)0;
        }
原文地址:https://www.cnblogs.com/nygfcn1234/p/3146326.html