IP-Address TextBox
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 OnKeyUp
, OnKeyPress
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 schreiben
int 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+=;
this.SelectionStart = iPos;
if(this.OverWriteMode)
{
if(cText[iPos] != ' ')
this.SelectionLength = ;
}
else
{
if(iPos < this.TextLength)
if(cText[iPos] == ' ')
this.SelectionLength = ;
}
}
}
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 schreiben
int iPos = this.SelectionStart;
char[] cText = this.Text.ToCharArray(); //Cursor hintern Punkt setzen
if((char.IsLetterOrDigit(Convert.ToChar(e.KeyValue)) ||
e.KeyCode == Keys.NumPad0)//Numpad0=96 --> `
&& iPos < this.TextLength)
{
if(cText[iPos] == '.' || cText[iPos] == ':'
|| cText[iPos] == '/')
iPos+=;
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 Zeichen
if(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+=) >= cText.Length)
break;
}
if(iPos < this.TextLength)
{
base.Text = this.Text.Substring(,iPos) +
" " + this.Text.Substring(iPos+);
this.SelectionStart = iPos+;
}
else
this.SelectionStart = this.TextLength-;
}
return true;
case Keys.Back:
if(iPos > )
{
while(cText[iPos-] == '.' ||
cText[iPos-] == ':' ||
cText[iPos-] == '/')
{
if((iPos-=)<=)
break;
}
if(iPos>)
{
base.Text = this.Text.Substring(,iPos-)
+ " " + this.Text.Substring(iPos);
this.SelectionStart = iPos-;
}
else
this.SelectionStart = ;
}
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.
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的更多相关文章
- ERROR 2003 (HY000): Can't connect to MySQL server on 'ip address' (111)的处理办法
远程连接mysql数据库时可以使用以下指令 mysql -h 192.168.1.104 -u root -p 如果是初次安装mysql,需要将所有/etc/mysql/内的所有配置文件的bind-a ...
- oracle 11g RAC安装节点二执行结果错误CRS-5005: IP Address: 192.168.1.24 is already in use in the network
[root@testdb11b ~]# /u01/app/oraInventory/orainstRoot.sh Changing permissions of /u01/app/oraInvento ...
- Assign an Elastic IP Address to Your Instance
By default, an instance in a nondefault VPC is not assigned a public IP address, and is private.You ...
- Ubuntu setup Static IP Address
Change Ubuntu Server from DHCP to a Static IP Address If the Ubuntu Server installer has set your se ...
- How to configure a static IP address on CentOS 7(CentOS7静态IP地址设置)
Question: On CentOS 7, I want to switch from DHCP to static IP address configuration with one of my ...
- Azure China (8) 使用Azure PowerShell创建虚拟机,并设置固定Virtual IP Address和Private IP
<Windows Azure Platform 系列文章目录> 本文介绍的是由世纪互联运维的Windows Azure China. 相比于Global Azure (http://www ...
- Windows Azure Cloud Service (44) 将Cloud Service加入Virtual Network Subnet,并固定Virtual IP Address(VIP)
<Windows Azure Platform 系列文章目录> 在之前的文章中,笔者已经详细介绍了如何将Virtual Machine加入Virtual Network,并且绑定固定的Pr ...
- [New Portal]Windows Azure Virtual Machine (19) 关闭Azure Virtual Machine与VIP Address,Internal IP Address的关系(1)
<Windows Azure Platform 系列文章目录> 默认情况下,通过Azure Management Portal创建的Public IP和Private IP都是随机分配的. ...
- [New Portal]Windows Azure Virtual Machine (20) 关闭Azure Virtual Machine与VIP Address,Internal IP Address的关系(2)
<Windows Azure Platform 系列文章目录> 默认情况下,通过Azure Management Portal创建的Public IP和Private IP都是随机分配的. ...
- Windows Azure Virtual Machine (28) 使用Azure实例级别IP,Instance-Level Public IP Address (PIP)
<Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China 熟悉Azure平台的读者都知道,我们在使用Azure Virtual ...
随机推荐
- git介绍
简介:Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目.Git 是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件.Git ...
- zabbix的配置使用
1 zabbix忘记密码的解决办法 zabbix的默认密码我们都知道是admin,zabbix都是存在与数据库中的 select * from zabbix.user where alias='adm ...
- html 语义化 资料
HTML: The Living Standard 理解HTML语义 什么是HTML语义化 为什么要语义化 写HTML代码时就注意什么 HTML 5的革新——语义化标签(一) header元素 foo ...
- SQL server 中的@,@@、#,##分别代表什么?
@声明变量,@@系统函数,#本地临时表,##全局临时 表
- 【USACO 2.1】Hamming Codes
/* TASK: hamming LANG: C++ URL:http://train.usaco.org/usacoprob2?a=5FomsUyB0cP&S=hamming SOLVE: ...
- APP图标和启动页
iOS App图标和启动画面尺寸 字数349 阅读22025 评论3 喜欢51 注意:iOS所有图标的圆角效果由系统生成,给到的图标本身不能是圆角的. 1. 桌面图标 (app icon) for i ...
- Numpy 用法小结
1. asarray 函数 可以将输入数据转化为矩阵格式. 输入数据可以是(列表,元组,列表的列表,元组的元组,元组的列表等这些数组形式). >>> asarray([(1,2,3 ...
- 内部类访问外部类的变量必须是final吗,java静态方法中不能引用非静态变量,静态方法中不能创建内部类的实例
内部类访问外部类的变量必须是final吗? 如下: package com.java.concurrent; class A { int i = 3; public void shout() { cl ...
- 【BZOJ-4569】萌萌哒 ST表 + 并查集
4569: [Scoi2016]萌萌哒 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 459 Solved: 209[Submit][Status] ...
- POJ2699 The Maximum Number of Strong Kings
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 2102 Accepted: 975 Description A tour ...