=========================================================================================
检查IP地址格式是否正确:
Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); //方式一 IPAddress address; if (!IPAddress.TryParse(txtIP.Text, out address)) //方式二 throw new Exception("IP地址格式不正确");
=========================================================================================
只允许文本框中输入数字:
private void txtNum_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar != 8 && !Char.IsDigit(e.KeyChar)) { e.Handled = true; } }
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
=========================================================================================
原文:https://www.cnblogs.com/mq0036/p/14113508.html