# VBForums CodeBank > CodeBank - Visual Basic .NET >  IP Textbox User Control

## kleinma

VS 2005/.NET 2.0

I made this user control to mimic the IP textbox in the Windows TCP/IP settings. It's different than a masked box or just straight custom validation, in that it is actually 4 textboxes that look like 1 textbox.

If you check the TCP/IP settings in Windows, you will see that this is how their box behaves. It works pretty well. Please let me know any comments/suggestions.

Attached is the full source code, and a sample project showing it in action. Since I did not include any compiled code, you need to build the project before you view form1.

----------


## Ginolard

That looks ideal!  How do I add that custom control to my project?

----------


## kleinma

From your project just select "Add existing item" and find the IPTextBox.vb file from my project. Once you add it to your project, build your project and the control will appear in your toolbox.

----------


## Robbrad182

Firstly Great stuff!! I tried to create a IP masked textbox in VB2005 using 099.099.099.099 but this didn't quite give me the result I was looking for.

Couple of suggestions for this but it could be what you mean by user controls.

1, When a . is pressed it moves to the next box or a backspace on the last char in a box moves you to the last box

2, Copy and paste function - ability to copy the content or paste content in 

If you could point me in the right direction that would be much appreciated

Thanks

----------


## Robbrad182

Fixed the when you press a dot it tabs I used


VB Code:
Private Sub txtIP_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtIP1.KeyPress, txtIP2.KeyPress, txtIP3.KeyPress, txtIP4.KeyPress
        Dim txtBox As TextBox = DirectCast(sender, TextBox)
        'ONLY ALLOW CONTROL CHARS (LIKE BACKSPACE) AND NUMBERS TO BE ENTERED
        Select Case Char.GetUnicodeCategory(e.KeyChar)
            Case Globalization.UnicodeCategory.Control, Globalization.UnicodeCategory.DecimalDigitNumber
            Case Else
                e.Handled = True
        End Select
        Debug.Print(e.KeyChar.ToString)
         If e.KeyChar = "." Then
            Me.SelectNextControl(txtBox, True, True, False, False)
        End If
    End Sub

Still not sure about the copy paste tho..

----------


## kleinma

do you mean copy/paste an entire IP into the box?

Since the user control was build to behave like the windows IP entry box, it is actually made up of 4 textboxes. So you can't copy/paste an entire IP. (you could, but you would have to write more code to look at a paste, see if its a full valid IP, and then fill in each of the 4 textboxes)

----------


## Robbrad182

I know thats the way the windows one acts but, it would be useful to copy and paste details as well as having the "." and the backspace function

I have made some adjustments to the project feel free to take a look

 :Smilie:

----------


## HanneSThEGreaT

Very nice indeed.

Can I give a suggestion ( use it / don't use it )

Why not make it so that you cannot enter 0 0 0 0 or 255 255 255 255 which an IP address cannot be. &#191;

----------


## JuggaloBrotha

> Very nice indeed.
> 
> Can I give a suggestion ( use it / don't use it )
> 
> Why not make it so that you cannot enter 0 0 0 0 or 255 255 255 255 which an IP address cannot be. ¿


Actually in Windows when it's obtaining an ip address or you use 'IpConfig /Release'  The system's IP is '0.0.0.0' and 255.255.255.255 is a valid ip, it's just not one you see

----------


## Robbrad182

Thanks guys for the good suggestions.

I think its really up to the user of the this feature as to whether they have some kind of validation on the values.

Cheers  :Wink:

----------


## Ant2ne

Ok, assuming I'm an idiot, how do I add this to my program?

----------


## kleinma

which version of Visual Studio?

----------


## Ginolard

One more thing.  How you add the functionality so that when the user presses ENTER it will run some sub/function?

----------


## manos364

I have added this sub as a new private sub, so every time you type Enter the current textbox transfers you to the next one.


vb Code:
Private Sub txtIP_KeyDown(ByVal sender As System.Object, ByVal e As KeyEventArgs) Handles txtIP1.KeyDown, txtIP2.KeyDown, txtIP3.KeyDown, txtIP4.KeyDown        Dim txtBox As TextBox = DirectCast(sender, TextBox)        If e.KeyCode = Keys.Enter Then            Me.SelectNextControl(txtBox, True, True, False, False)        End If    End Sub

----------


## jespa007

Thanks very much for this component! It was very useful to control any input ip entry.
I'm glad to meet people that makes our lives easy  :Smilie: 

Thanks again kleinma  :Smilie:  :Smilie:

----------


## Titanium105

Hi! 

Just stopping in to say i like the custom control! 

I know this is an old thread, but i was wondering how can you change the default values on this, i would like to see all blanks instead of 255.255.255.255

----------

