根据网上的改写:http://blog.csdn.net/jhqin/article/details/5823363

控件属性:

Text:获取或设置string类型的IP地址

Value:获取或设置IPAddress类型的IP地址

Type:获取IP地址所属分类。分A、B、C、D、E 5大类。

BorderStyle:获取或设置控件的边框样式。

ValidateIP() 判断ip是否有效
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;namespace FVD.Common
{public enum IPType : byte { A, B, C, D, E };public class IPAddressTextBox : 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> /// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary> /// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region 组件设计器生成的代码/// <summary> /// 设计器支持所需的方法 - 不要/// 使用代码编辑器修改此方法的内容。/// </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.Location = new System.Drawing.Point(0, 2);this.textBox1.MaxLength = 3;this.textBox1.Name = "textBox1";this.textBox1.Size = new System.Drawing.Size(25, 21);this.textBox1.TabIndex = 0;this.textBox1.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);// // textBox2// this.textBox2.Location = new System.Drawing.Point(33, 2);this.textBox2.MaxLength = 3;this.textBox2.Name = "textBox2";this.textBox2.Size = new System.Drawing.Size(25, 21);this.textBox2.TabIndex = 1;this.textBox2.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);// // textBox3// this.textBox3.Location = new System.Drawing.Point(67, 2);this.textBox3.MaxLength = 3;this.textBox3.Name = "textBox3";this.textBox3.Size = new System.Drawing.Size(25, 21);this.textBox3.TabIndex = 2;this.textBox3.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;this.textBox3.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.IPv4TextBox_KeyPress);// // textBox4// this.textBox4.Location = new System.Drawing.Point(100, 2);this.textBox4.MaxLength = 3;this.textBox4.Name = "textBox4";this.textBox4.Size = new System.Drawing.Size(25, 21);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.AutoSize = true;this.label1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.label1.Location = new System.Drawing.Point(25, 5);this.label1.Name = "label1";this.label1.Size = new System.Drawing.Size(16, 16);this.label1.TabIndex = 4;this.label1.Text = ".";this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;// // label2// this.label2.AutoSize = true;this.label2.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.label2.Location = new System.Drawing.Point(57, 5);this.label2.Name = "label2";this.label2.Size = new System.Drawing.Size(16, 16);this.label2.TabIndex = 5;this.label2.Text = ".";this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;// // label3// this.label3.AutoSize = true;this.label3.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));this.label3.Location = new System.Drawing.Point(92, 5);this.label3.Name = "label3";this.label3.Size = new System.Drawing.Size(16, 16);this.label3.TabIndex = 6;this.label3.Text = ".";this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;// // IPAddressTextBox// this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.Controls.Add(this.textBox4);this.Controls.Add(this.textBox3);this.Controls.Add(this.textBox2);this.Controls.Add(this.label3);this.Controls.Add(this.label2);this.Controls.Add(this.label1);this.Controls.Add(this.textBox1);this.Name = "IPAddressTextBox";this.Size = new System.Drawing.Size(125, 28);this.ResumeLayout(false);this.PerformLayout();}#endregionpublic IPAddressTextBox(){InitializeComponent();}private void IPv4TextBox_KeyPress(object sender, KeyPressEventArgs e){char KeyChar = e.KeyChar;int TextLength = ((TextBox)sender).TextLength;if (KeyChar == '.' || KeyChar == '。' || KeyChar == ' '){if ((((TextBox)sender).SelectedText.Length == 0) && (TextLength > 0) && (((TextBox)sender) != textBox4)){   // 进入下一个文本框SendKeys.Send("{Tab}");}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){   // 回退到上一个文本框 Shift+TabSendKeys.Send("+{TAB}{End}");}                        }}else{e.Handled = true;}}}/// <summary>/// string类型的IP地址/// </summary>override public 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");}}                }}/// <summary>/// IP地址/// </summary>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");}}}/// <summary>/// IP地址分类/// </summary>public IPType Type{get{byte[] bytes = this.Value.GetAddressBytes();int FirstByte = bytes[0];if (FirstByte < 128){return IPType.A;}else if (FirstByte < 192){return IPType.B;}else if (FirstByte < 224){return IPType.C;}else if (FirstByte < 240){return IPType.D;}else{return IPType.E;    // 保留做研究用
                }}}public bool ValidateIP(){IPAddress address;string ipString = textBox1.Text + "." + textBox2.Text + "." + textBox3.Text + "." + textBox4.Text;return IPAddress.TryParse(ipString, out address);}/// <summary>/// 控件的边框样式/// </summary>new public BorderStyle BorderStyle{get{return this.textBox1.BorderStyle;}set{for (int i = 1; i <= 4; i++){((TextBox)this.Controls["textBox" + i.ToString()]).BorderStyle = value;}}}}
}

转载于:https://www.cnblogs.com/jhlong/p/5534723.html

C#创建用户控件 - IPv4地址输入框相关推荐

  1. (winform)创建用户控件以及用户控件的使用

    (1)创建用户控件 下面以创建一个计时器控件为例.首先创建"Windows窗体控件库"项目 如下图,创建了一个名为"Clocker"的窗体控件项目. 我这个工程 ...

  2. Windows Phone 7 不温不火学习之《创建用户控件》

    同样出自微软的产品,像ASP.NET 一样,Windows Phone 7 也有一个叫UserControl 的东西.这个相当于一个组件,类似于Android 继承View . 本篇将实现一个用户控件 ...

  3. WPF 用户控件分享之边上带输入框的圆圈

    WPF 用户控件分享之边上带输入框的圆圈 独立观察员 2022 年 8 月 20 日 最近有这样一个需求,有一圈圆形,每个圆形边上有个输入框,以下是完成后的效果图: 拿到这个需求后,分析界面上每个圆形 ...

  4. 一步一步学Silverlight 2系列(10):使用用户控件

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  5. ASP.NET的用户控件

    本文介绍如何在ASP.NET中创建用户控件,控件属性的动态修改以及控件的事件出发机制. 简介 ASP.NET的服务端控件使得Web开发工作变得更为简单,功能更为强大.我们介绍过如何在ASP.NET页面 ...

  6. 学习笔记---母板页、用户控件、第三方控件及视图状态管理

    一.母版页 在制作页面的过程中, 多个页面往往具有相同的页面Header和页面Footer, 多个页面只是在中间部分有变化. 那么我们完全可以避免在每个页面中都写一遍页头和页尾的代码, 这种技术就是母 ...

  7. 用户控件和自定义控件

    关 键 词 Server Control 服务器控件 User Control 用户控件,ASP.NET服务器控件的一种(一般后缀名为.ASCX文件) Custom Control 自定义控件,ASP ...

  8. 开发和使用Web用户控件

    在 ASP.NET 的开发中 Web 用户控件的开发和使用是一项必不可少的技术,在对这项技术的一番研究后写下了这篇随笔,不过确实担心这么初级的东东放到原创首页上会被拍砖头. 1.简介 2.创建 Web ...

  9. (转) ASP.NET 2.0:使用用户控件和定制的Web部件个人化你的门户网站(二)

    Web部件目录 我们已经见过了如何在 WebPartZones 控件中事先放入Web部件.你还可以用另外一种方法完成这个功能,那就是允许用户在运行时添加新的Web部件.通过使用 CatalogZone ...

最新文章

  1. Yolo:实时目标检测实战(下)
  2. 用silverlight做动画-相机
  3. 【专访】小米产品经理颠覆早教行业,欲送给孩子1000万美金的人生
  4. boost::multiprecision模块将 std::numeric_limits 用作 multiprecision.qbk 上的多精度文档片段的示例
  5. 重磅!李宏毅教授机器学习训练营
  6. step1 . day6 C语言基础练习之数组和字符串
  7. oracle trace文件解读
  8. 这些年遇到的坑爹问题汇总
  9. JAVA确保垃圾回收后结束程序_Java垃圾回收机制(转)
  10. Zabbix 安装配置
  11. Atitit 音频技术简史艾提拉著 目录 1. 2014年1月16日,谷歌发布音乐时间轴 2 2. 时代发展 2 2.1. 机械录音 电声录音时代 四.数码录音时代 2 3. 【音频录音技术】 2
  12. html背景纯白,纯白色背景图片全白
  13. 欢迎使用CSDN论坛阅读器 : CSDN Reader(附全部源代码) 最新版本:20110222
  14. 关于DCMM评估模型的全面解析
  15. 吐血总结让你的项目管理水平提升最快的19种顶级思维
  16. 魅族怎么更改html,魅族默认浏览器设置
  17. 歌手详情页:下拉方大歌手图片
  18. 实验室方法检出限和定量限标准做法
  19. C语言函数体内无条件的大括号
  20. shopee入驻条件费用-shopee 2020年最新的入驻须知

热门文章

  1. ios图片轮播 (基础篇——UIScrollView实现方式)
  2. PHP CURL 异步测试
  3. Activiti手动执行的应用(UserTask)
  4. .9-浅析express源码之请求处理流程(2)
  5. jacascript 立即执行函数(IIFE)与闭包
  6. 关于cocoa框架,你所要知道的一切(苹果官方文档,cocoa框架核心竞争力,必须收藏!)...
  7. Android 5.0新特性
  8. 电子商务网站 数据库产品表设计方案
  9. IntelliJ IDEA 2021连接MySql数据库的操作
  10. 7-1 堆栈操作合法性 (15 分)