Http://www.codeproject.com/Articles/4693/IP-Address-TextBox
You can download the trial effect. Personal feeling is very powerful, but the input is not very comfortable. can refer to.
Ntroduction
Problem was, I didn ' t find a solution to edit the IP address like in the Windows network environment, for C #. Although there is some controls for masked edit fields, I wanted to write my own, and if so I wanted it to behave like th e controls from the MFC library or Windows network environment and maybe a little more.
Problems to solve
The heaviest problem at writing the control is to catch the inputs of backspace and delete keys, to delete characters fro m the input field. I tried a lot with overridden event handlers, and that OnKeyDown
OnKeyUp
it didn ' t work like it should.
Then I remembered this another developer had overridden the PreProsessMessage
method to catch keyboard inputs and handle it in own it S. So I implemented PreProcessMessage
a override for to handle all the backspaces and delete key presses and used OnKeyUp
, and OnKeyPress
to handle the inputs of dots and slashes and sets the input cursor to the right position.
OnKeyDown Event
/// <summary>///Override Standard Keydowneventhandler///Catches Inputs of "." and "/" to the jump to next positions/// </summary>/// <param name= "E" >keyeventargument</param>protected Override voidOnKeyDown (KeyEventArgs e) {//Zeichen a die richtige Stelle schreiben intIPos = 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 the jump to next positions/// </summary>/// <param name= "E" >keyeventargument</param>protected Override voidOnKeyUp (KeyEventArgs e) {//Zeichen a die richtige Stelle schreiben intIPos = 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+=1; This. SelectionStart =IPos; } Base. OnKeyUp (e);}
OnKeyPress Event
/// <summary>///Override Standard Keypresseventhandler///Catches Inputs of "." and "/" to the jump to next positions/// </summary>/// <param name= "E" >keypresseventargument</param>protected Override voidOnKeyPress (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 BOOLpreProcessMessage (refMessage msg) { if(Msg. MSG = =wm_keydown) {Keys KeyData= (Keys) (int) Msg. WParam) |ModifierKeys; Keys KeyCode= (Keys) (int) Msg. WParam); intIPos = This. SelectionStart; Char[] Ctext = This. Text.tochararray (); Switch(keycode) { CaseKeys.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; } Else This. SelectionStart = This. textlength-1; } return true; CaseKeys.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; } Else This. SelectionStart =0; } return true; default: Break; } } return Base. preProcessMessage (refmsg);}
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 hard coded.
Hide Copy Code
if (char. Isletterordigit (Convert.tochar (e.keyvalue)) | | E.keycode = = keys.numpad0)//numpad0=96 ---"this. TextLength) {[...]}
At least ...
... I have a control over looks like a with TextBox
dots, where I can input numbers, the type dots to jump to next IP parts, and get Its contents via the property Text
.
Using the Code
Include the IPAddressTextBox.cs in your project. Set A in TextBox
your form or the user control and clear its contents. The change of the type of the is in TextBox
System.Windows.Forms.TextBox
rj2_cs.IPAddressTextBox
code Editor. Then your can change the properties of the IP textbox like you want.
Ip-address TextBox