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; }