using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace 验证IP合法性 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { try { string[] format = new string[4]; string s = "."; format = textBox1.Text.Split(s.ToCharArray(), 4); for (int i = 0; i < 4; i++) { if (Convert.ToInt32(format[i]) >= 255) { MessageBox.Show("IP地址不合法"); if (Convert.ToInt32(format[i]) <= 255) { MessageBox.Show("IP地址正确"); } } MessageBox.Show("IP地址正确"); } } catch { MessageBox.Show("IP地址不合法"); } } private void Form1_Load(object sender, EventArgs e) { } } }