C# IPv4 控件
来源:互联网 发布:学粤语 知乎 编辑:程序博客网 时间:2024/04/28 09:36
调用时 直接使用 private IPv4TextBox XXX;
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Net;using System.Text;using System.Text.RegularExpressions;using System.Windows.Forms;namespace BASARA{ class IPv4TextBox : UserControl { private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.TextBox textBox3; private System.Windows.Forms.TextBox textBox4; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } public IPv4TextBox() { InitializeComponent(); } /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.textBox1 = new System.Windows.Forms.TextBox(); this.textBox2 = new System.Windows.Forms.TextBox(); this.textBox3 = new System.Windows.Forms.TextBox(); this.textBox4 = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label3 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // textBox1 // this.textBox1.BackColor = System.Drawing.SystemColors.Window; this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox1.Location = new System.Drawing.Point(0, 0); this.textBox1.MaxLength = 3; this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(100, 12); this.textBox1.TabIndex = 0; this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress); this.textBox1.KeyUp += new System.Windows.Forms.KeyEventHandler(this.IPv4TextBox_KeyUp); // // textBox2 // this.textBox2.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox2.Location = new System.Drawing.Point(0, 0); this.textBox2.MaxLength = 3; this.textBox2.Name = "textBox2"; this.textBox2.Size = new System.Drawing.Size(100, 12); this.textBox2.TabIndex = 1; this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress); this.textBox2.KeyUp += new System.Windows.Forms.KeyEventHandler(this.IPv4TextBox_KeyUp); // // textBox3 // this.textBox3.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox3.Location = new System.Drawing.Point(0, 0); this.textBox3.MaxLength = 3; this.textBox3.Name = "textBox3"; this.textBox3.Size = new System.Drawing.Size(100, 12); this.textBox3.TabIndex = 2; this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress); this.textBox3.KeyUp += new System.Windows.Forms.KeyEventHandler(this.IPv4TextBox_KeyUp); // // textBox4 // this.textBox4.BorderStyle = System.Windows.Forms.BorderStyle.None; this.textBox4.Location = new System.Drawing.Point(0, 0); this.textBox4.MaxLength = 3; this.textBox4.Name = "textBox4"; this.textBox4.Size = new System.Drawing.Size(100, 12); this.textBox4.TabIndex = 3; this.textBox4.TextAlign = System.Windows.Forms.HorizontalAlignment.Center; this.textBox4.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress); // // label1 // this.label1.Location = new System.Drawing.Point(0, 0); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(7, 12); this.label1.TabIndex = 4; this.label1.Text = "."; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // label2 // this.label2.Location = new System.Drawing.Point(0, 0); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(7, 12); this.label2.TabIndex = 5; this.label2.Text = "."; this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // label3 // this.label3.Location = new System.Drawing.Point(0, 0); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(7, 12); this.label3.TabIndex = 6; this.label3.Text = "."; this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // IPv4TextBox // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D; this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.textBox4); this.Controls.Add(this.textBox3); this.Controls.Add(this.textBox2); this.Controls.Add(this.textBox1); this.Name = "IPv4TextBox"; this.Size = new System.Drawing.Size(146, 21); this.ResumeLayout(false); this.PerformLayout(); } private void IPv4TextBox_KeyPress(object sender, KeyPressEventArgs e) { char keyChar = e.KeyChar; int textLength = ((TextBox)sender).TextLength; if (keyChar == '.' || keyChar == ' ') { if ((((TextBox)sender).SelectedText.Length == 0) && (textLength > 0) && (((TextBox)sender) != textBox4)) { SendKeys.Send("{Tab}"); ((TextBox)(this.GetNextControl(this.ActiveControl, true))).SelectAll(); } e.Handled = true; } if (Regex.Match(keyChar.ToString(), "[0-9]").Success) { if (textLength == 2) { if (int.Parse(((TextBox)sender).Text + e.KeyChar.ToString()) > 255) { e.Handled = true; } } else if (textLength == 0) { if (keyChar == '0') { e.Handled = true; } } } else { if(keyChar == '\b') { if(textLength == 0) { if(((TextBox)sender) != textBox1) { SendKeys.Send("+{TAB}{End}"); } } } else { e.Handled = true; } } } private void IPv4TextBox_KeyUp(object sender, KeyEventArgs e) { int textLength = ((TextBox)sender).TextLength; if (textLength == 3 && ((TextBox)sender).SelectedText.Length == 0) { SendKeys.Send("{Tab}"); ((TextBox)(this.GetNextControl(this.ActiveControl, true))).SelectAll(); e.Handled = true; } base.OnKeyUp(e); } public override string Text { get { return this.Value.ToString(); } set { IPAddress address; if(IPAddress.TryParse(value, out address)) { byte[] bytes = address.GetAddressBytes(); for(int i = 1; i <= 4; i++) { this.Controls["textBox" + i.ToString()].Text = bytes[i-1].ToString("D"); } } } } public IPAddress Value { get { IPAddress address; string ipString = textBox1.Text + '.' + textBox2.Text + '.' + textBox3.Text + '.' + textBox4.Text; if(IPAddress.TryParse(ipString, out address)) { return address; } else { return new IPAddress(0); } } set { byte[] bytes = value.GetAddressBytes(); for(int i = 1; i <= 4; i++) { this.Controls["textBox" + i.ToString()].Text = bytes[i-1].ToString("D"); } } } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); System.Drawing.Size textBoxSize = new System.Drawing.Size((this.Size.Width - 33) / 4, 19); this.textBox1.Size = textBoxSize; this.textBox2.Size = textBoxSize; this.textBox3.Size = textBoxSize; this.textBox4.Size = textBoxSize; this.textBox1.Location = new System.Drawing.Point(0, 5); this.label1.Location = this.textBox1.Location + new System.Drawing.Size(textBoxSize.Width + 2, 0); this.textBox2.Location = this.label1.Location + new System.Drawing.Size(9, 0); this.label2.Location = this.textBox2.Location + new System.Drawing.Size(textBoxSize.Width + 2, 0); this.textBox3.Location = this.label2.Location + new System.Drawing.Size(9, 0); this.label3.Location = this.textBox3.Location + new System.Drawing.Size(textBoxSize.Width + 2, 0); this.textBox4.Location = this.label3.Location + new System.Drawing.Size(9, 0); } }}
0 0
- C# IPv4 控件
- C#创建用户控件:IPv4地址输入框
- C#创建用户控件:IPv4地址输入框
- C#实现 获取IPv4
- C# 获得IPV4地址
- C# 获取IPv4
- C#获取本机IPv4
- C# 检查IPv4地址是否合法
- c#正则表达式 ipv4地址
- IPv4
- IPv4
- C#获取本机IPv4地址
- c#使用正则表达式匹配ipv4地址
- C#获取本机IPv4地址
- c# 机器名和IP取得(IPV4 IPV6)
- C#获取本机IP地址(ipv4)
- c# 多网卡获取ipv4 ip地址办法列举
- C# 获取IPV4地址,网关,子网掩码,网卡信息的方法
- 关于Python的super用法研究
- C# 打开以对话框,获取文件夹路径 、文件的路径、文件名
- 如何让内置sdcard不弹出 存储设备插入拔出消息。
- sql 查找节点下的所有子节点,查找节点的所有父节点
- linux下 的vi的使用方法
- C# IPv4 控件
- UrlRewrite
- 属性表修改
- iOS 实现类似安卓.9.png图片拉伸效果
- 隐藏 运行批处理(bat)的命令行(cmd)窗口
- 使用make命令编译项目文件入门
- 加密算法
- Win10系统下激活系统和office2013教程
- 转】深度学习--深度信念网络(Deep Belief Network)