http://www.codeproject.com/Articles/4693/IP-Address-TextBox

可以下载试用效果。个人感觉功能很强大,但输入时让人不太舒服。可以参考。

ntroduction

Problem was, I didn't find a solution to edit an IP address like in Windows network environment, for C#. Although there are some controls for masked edit fields, I wanted to write my own, and if so I wanted it to behave like the controls from MFC library or Windows network environment and maybe a little more.

Problems to solve

The heaviest problem at writing the control was to catch the inputs of backspace and delete keys, to delete characters from the input field. I tried a lot with overridden event handlers, OnKeyDown and OnKeyUp but it didn't work like it should.

Then I remembered that another developer had overridden the PreProsessMessage method to catch keyboard inputs and handle it in own ways. So I implemented an override for PreProcessMessage to handle all the backspaces and delete key presses and used OnKeyUpOnKeyPress and OnKeyDown to handle the inputs of dots and slashes and set the input cursor to the right position.

OnKeyDown event

/// <summary>
/// Override standard KeyDownEventHandler
/// Catches Inputs of "." and "/" to jump to next positions
/// </summary>
/// <param name="e">KeyEventArgument</param>protected override void OnKeyDown(KeyEventArgs e)
{//Zeichen an die richtige stelle schreibenint iPos = this.SelectionStart;char[] cText = this.Text.ToCharArray();if(e.Modifiers == Keys.None){if((char.IsLetterOrDigit(Convert.ToChar(e.KeyValue)) || e.KeyCode == Keys.NumPad0)//Numpad0=96 --> `&& iPos < this.TextLength){if(cText[iPos] == '.' || cText[iPos] == ':'|| cText[iPos] == '/')iPos+=1;this.SelectionStart = iPos;if(this.OverWriteMode){if(cText[iPos] != ' ')this.SelectionLength = 1;}else{if(iPos < this.TextLength)if(cText[iPos] == ' ')this.SelectionLength = 1;}}}base.OnKeyDown (e);
}

 

OnKeyUp event

/// <summary>
/// Override standard KeyUpEventHandler
/// Catches Inputs of "." and "/" to jump to next positions
/// </summary>
/// <param name="e">KeyEventArgument</param>protected override void OnKeyUp(KeyEventArgs e)
{//Zeichen an die richtige stelle schreibenint iPos = this.SelectionStart;char[] cText = this.Text.ToCharArray();//Cursor hintern Punkt setzenif((char.IsLetterOrDigit(Convert.ToChar(e.KeyValue)) || e.KeyCode == Keys.NumPad0)//Numpad0=96 --> `&& iPos < this.TextLength){if(cText[iPos] == '.' || cText[iPos] == ':' || cText[iPos] == '/')iPos+=1;this.SelectionStart = iPos;}base.OnKeyUp (e);
}

OnKeyPress event

/// <summary>
/// Override standard KeyPressEventHandler
/// Catches Inputs of "." and "/" to jump to next positions
/// </summary>
/// <param name="e">KeyPressEventArgument</param>protected override void OnKeyPress(KeyPressEventArgs e)
{//Zulassige Zeichenif(char.IsControl(e.KeyChar) ||m_regexValidNumbers.IsMatch(e.KeyChar.ToString())){e.Handled = false;}else{switch(e.KeyChar){case '/':this.JumpToSlash();break;case '.':this.JumpToNextDot();break;default:break;}e.Handled = true;}base.OnKeyPress(e);
}

PreProcessMessage

/// <summary>
/// Override standard PreProcessMessage
/// Catches Inputs of BackSpace and Deletes
/// </summary>
/// <param name="msg">PreProcessMessage</param>public override bool PreProcessMessage(ref Message msg)
{if (msg.Msg == WM_KEYDOWN){Keys keyData = ((Keys) (int) msg.WParam) |ModifierKeys;Keys keyCode = ((Keys) (int) msg.WParam);int iPos = this.SelectionStart;char[] cText = this.Text.ToCharArray();switch(keyCode){case Keys.Delete:if(iPos < this.TextLength){while(cText[iPos] == '.' || cText[iPos] == ':' || cText[iPos] == '/'){if((iPos+=1) >= cText.Length)break;}if(iPos < this.TextLength){base.Text = this.Text.Substring(0,iPos) + " " + this.Text.Substring(iPos+1);this.SelectionStart = iPos+1;}elsethis.SelectionStart = this.TextLength-1;}return true;case Keys.Back:if(iPos > 0){while(cText[iPos-1] == '.' || cText[iPos-1] == ':' || cText[iPos-1] == '/'){if((iPos-=1)<=0)break;}if(iPos>0){base.Text = this.Text.Substring(0,iPos-1) + " " + this.Text.Substring(iPos);this.SelectionStart = iPos-1;}elsethis.SelectionStart = 0;}return true;default:break;}    }return base.PreProcessMessage (ref msg);
}

Another problem was the input of numbers via the numpad. Especially the 0 key was not recognized, because it's char value is neither a letter nor a digit, so I had to ask for Keys.NumPad0 hard coded.

Hide   Copy Code
if((char.IsLetterOrDigit(Convert.ToChar(e.KeyValue)) || e.KeyCode == Keys.NumPad0)//Numpad0=96 --> `iPos < this.TextLength)
{[...]}

At least...

...I have a control that looks like a TextBox with dots, where I can input numbers, type dots to jump to next IP parts, and get its contents via the Text property.

Using the code

Include the IPAddressTextBox.cs in your project. Set a TextBox in your form or user control and clear its contents. Change the type of this TextBox from System.Windows.Forms.TextBox to rj2_cs.IPAddressTextBox in code editor. Then you can change the properties of the IP textbox like you want.

IP-Address TextBox相关推荐

  1. 1108. Defanging an IP Address

    1108. Defanging an IP Address 题目描述 Example 1: Input: address = "1.1.1.1" Output: "1[. ...

  2. Windows Azure Virtual Network (6) 设置Azure Virtual Machine固定公网IP (Virtual IP Address, VIP) (1)...

    <Windows Azure Platform 系列文章目录> 注意:本文介绍的是Global Azure (http://www.windowsazure.com),如果你使用的是由世纪 ...

  3. 关于“INS-40922 Invalid Scan Name – Unresolvable to IP address”

    今天在虚拟机上安装rac11.2.0.4的时候遇到报错"INS-40922 Invalid Scan Name – Unresolvable to IP address"其实这个问 ...

  4. dubbo could not get local host ip address will use 127.0.0.1 instead 异常处理

    dubbo could not get local host ip address will use 127.0.0.1 instead 异常处理 参考文章: (1)dubbo could not g ...

  5. 使用访问控制列表预防IP地址欺骗(IP Address Spoofing Prevention with ACLs)

    [实验说明] 配置路由器预防IP地址欺骗   [实验拓扑] IOS:c2691-advsecurityk9-mz.124-11.T2.bin [实验配置步骤]   在网关的outside过滤RFC19 ...

  6. Windows azure国际版下通过 windows auzre powershell为VM分配绑定virtual ip address

    说到windows azure,相信大家已经对此产品已经相当熟悉了,但使用的版本最多的应该是windows azure国内版本,也就是说,是微软委派世纪互联提供服务的平台,登陆地址为:https:// ...

  7. auto drop ssh failed ip address

    #/bin/bash #auto drop ssh failed ip address #author by efoni 2018.7 SEC_FILE=/var/log/secure #如下为截取s ...

  8. web笔记Error:That IP address

    前言 题主最近在做一个web服务 在实现过程中有苦有乐,现在记录一下,既方便自己以后回顾,也方便后来的人. 具体细节 在实现过程终遇到了下面的问题: >python manage.py runs ...

  9. Warning: Permanently added the RSA host key for IP address '13.229.188.59' to the list of known host

    前言 最近在学习GIt,安装了Git bash,过程中遇到了各种问题,写下帖子,方便大家解决问题,也方便自己回顾 问题描述 Warning: Permanently added the RSA hos ...

  10. lo ate my IP address问题解决

    项目中遇到一个奇怪的问题,redis数据库在系统启动的时候,会引起阻塞的问题,现象是应用访问redis数据库阻塞住,过一会自动恢复. 网上也没找到相关具体说明和解决方法,在一个地方看到有描述MASQU ...

最新文章

  1. OpenvSwitch代码分析之bridge和port
  2. 手机的基本功能测试---情景模式
  3. mysql并发写入性能分析
  4. Spring Boot(十三):整合Redis哨兵,集群模式实践
  5. 我的Oracle 9i学习日志(18)-- 维护数据完整性.b
  6. web报表工具FineReport的公式编辑框的语法简介
  7. 在java中使用关键字导入包_java的import关键字的使用
  8. checkbox全选
  9. 移植Andorid4.0.4 - 步骤及问题汇总
  10. 类型的设计--方法:构造器、操作符、转换操作符和参数
  11. 批量给 Word、PPT 和 PDF 插入封面或新的页面
  12. SSH远程控制计算机
  13. 那些年面挂的js手写题
  14. 电子钱包电子存折,区别
  15. #torch.bmm()函数解读
  16. 计算机网卡更改mac地址,苹果MAC地址怎么改?MAC网卡物理地址修改的详细方法
  17. python含义是什么_python _=是什么意思
  18. ARMv7 汇编在线调试工具
  19. background各个属性详解
  20. 【夏目鬼鬼分享】rabbitmq消息队列发送于接收

热门文章

  1. LVM 逻辑卷的扩容/缩容
  2. 【系列】EOS开发1 开发环境搭建
  3. 不仅仅是手机,MWC现全球首例 5G NR 商用部署
  4. 为Spring Cloud Config Server配置远程git仓库
  5. bash魔法堂:History用法详解
  6. Mysql5.6主从复制-基于binlog
  7. [转] java的 Collection 和 Map 详解
  8. 取代Python多进程!伯克利开源分布式框架Ray
  9. 一文读懂MySQL事务锁、事务级别
  10. MaxCompute助力ofo实现精细化运营:日订单超3200万、整体运行效率提升76%