Hello,

I want to have a manual IP-input-box like windows.

tcpip_properties.gif

I tried by MaskedTextBox but the problem is, the 3 pionts are sliding when i insert some numbers and i can insert letters and numbers higher then 255 (higher is not allowed in IP´s).

I hope anyone helps me, maybe with some codesnips!?! :)

best regards
josh

Recommended Answers

All 8 Replies

These examples doesn´t helped ;(

So here is my code : on the form is only a MaskedTextBox and a Button.

Option Strict On
Imports System
Imports System.Net.NetworkInformation
Imports System.IO
Imports System.Text
Imports System.ComponentModel
Imports System.Management
Imports System.Xml
Imports System.Text.RegularExpressions

Public Class Form1
    Dim myIPValue As String

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MaskedTextBox1.Mask = "###.###.###.###"
        MaskedTextBox1.PromptChar = Chr(32) 'space character
    End Sub

    Private Sub MaskEdBox1_KeyPress(KeyAscii As Integer)
        Dim mySplit() As String, c As Integer, jumpC As Integer, tmpString As String, tmpSplit As String
        If KeyAscii = 46 Then
            mySplit = Split(MaskedTextBox1.Text, Chr(46), -1)
            For c = 0 To UBound(mySplit)
                If IsNumeric(mySplit(c)) Then jumpC = jumpC + 4
                If Len(Trim(mySplit(c))) < 3 Then mySplit(c) = Space(3 - Len(Trim(mySplit(c)))) & Trim(mySplit(c))
            Next c
            tmpString = mySplit(0) & "." & mySplit(1) & "." & mySplit(2) & "." & mySplit(3)
            MaskedTextBox1.Text = tmpString
            MaskedTextBox1.SelectionStart = jumpC
            KeyAscii = 0
        End If
    End Sub

    Private Sub MaskEdBox1_KeyUp(KeyCode As Integer, Shift As Integer)
        Dim validSplit() As String, v As Integer, validString As String
        validSplit = Split(MaskedTextBox1.Text, Chr(46), -1)
        For v = 1 To UBound(validSplit)
            If validSplit(v) = "" Or v = UBound(validSplit) Then
                If Val(Trim(validSplit(v - 1))) > 255 Or (v = UBound(validSplit) And Val(Trim(validSplit(v))) > 255) Then
                    MsgBox("Invalid value")
                    If Val(validSplit(3)) > 0 Then v = v + 1
                    validSplit(v - 1) = ""
                    validString = validSplit(0) & "." & validSplit(1) & "." & validSplit(2) & "." & validSplit(3)
                    MaskedTextBox1.Text = validString
                    MaskedTextBox1.SelectionStart = (v - 1) * 4
                    Exit For
                End If
            End If
        Next v
        myIPValue = Replace(MaskedTextBox1.Text, " ", "")
    End Sub

    Private Sub changeIP_Button_Click(sender As Object, e As EventArgs) Handles changeIP_Button.Click
        'Code to change IP will follow, if no more problems with inputbox...

    End Sub

End Class

The problem is, that i can still input something like "9 9.999.99. 9" this numbers are not allowed because it is over 255, there are spaces and the points are not all the time at the same position - they are sliding.

I attached my project as zip.

Since you don't want to see how others solved it, then you have to put more tests into your code to see if the value so far is valid and if not, take what action your feel is needed. Either delete the last character or BSOD.

Why not start with the simplest case. Create a textbox for one set of the IP address digits and use the keystroke events to restrict entry to only digits and values from 0-255. Once you have the logic for that you can expand to the full case. If you know how, you can subclass the TextBox control and build up a new IP address control from there.

You may want to add a user control, with 4 or 6 textboxes depending on IPv4 or IPv6. For example:

Public Class IP_Textbox
    Const nBytes As Int32 = 4 ' IPv4 (change to =6 if IPv6)
    Dim vTextbox(nBytes - 1) As TextBox
    Dim vLabel(nBytes - 2) As Label
    Private Sub IP_Textbox_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            Me.BackColor = Color.White
            Dim left As Int32 = 4
            For i As Int32 = 0 To nBytes - 1
                vTextbox(i) = New TextBox
                With vTextbox(i)
                    .Width = 25
                    .TextAlign = HorizontalAlignment.Center
                    .BorderStyle = BorderStyle.None
                    .BackColor = Color.White
                    .MaxLength = 3
                    .Location =
                        New Point(left, 3 + (Me.Height - vTextbox(i).Height) / 2)
                    AddHandler .Validating, AddressOf vTextChanged
                    left += 25
                End With
                Me.Controls.Add(vTextbox(i))
                If i < nBytes - 1 Then
                    vLabel(i) = New Label
                    With vLabel(i)
                        .AutoSize = True
                        .Text = ","
                        .BorderStyle = BorderStyle.None
                        .BackColor = Color.White
                        .Location = New Point(left, 7)
                        left += 10
                    End With
                    Me.Controls.Add(vLabel(i))
                End If
            Next
            Me.Width = left + 6
            Me.Height += 2
        Catch ex As Exception

        End Try
    End Sub
    Public ReadOnly Property IP As String()
        Get
            Dim vText(vTextbox.Length - 1) As String
            Try
                For i As Int32 = 0 To vText.Length - 1
                    vText(i) = vTextbox(i).Text
                Next
            Catch ex As Exception

            End Try
            Return vText
        End Get
    End Property
    Private Sub vTextChanged(sender As Object, e As System.ComponentModel.CancelEventArgs)
        Try
            Dim tb As TextBox = CType(sender, TextBox)
            tb.Text = Trim(tb.Text)
            If Len(tb.Text) = 0 Then tb.Text = "0"
            Dim ip As Int32
            If Int32.TryParse(tb.Text, ip) AndAlso
            0 <= ip AndAlso ip <= 255 Then
                Exit Try
            Else
                tb.Text = ip.ToString
            End If
            e.Cancel = True
        Catch ex As Exception

        End Try
    End Sub
End Class

IP_Textbox.PNG

IP_Textbox2.PNG

I've made several improvents to the code, so here is another version.

Thank you, it works.

I adjoint the project once again because I found it was throwing errors, did you find any? If you find any further error please let me know.

IP_Textbox3.PNG

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.