How would i make an input mask for an IP Address where if you press period it will go to the next octet i.e. you can type 192 (period) 168 (period) 0 (period) 1 and it will return 192.168.0.1 I know that I can use 099.099.099.099 as my input mask, but then they have to put in all leading zero's or cursor over. Any ideas

Input Mask for IP Address
hussain hammad
For anyone who cares, I figured out a solution. Well, I ended up writing some code instead of using an input mask. It works the same way, maybe even better. It will automatically add a decimal if you type in three characters, or let you put them in manually. It won't let you put two decimals next to eachother, and if you have an octet larger than 255 it will become 255. Just paste the following code, replacing TextBox1 with whatever your text boxe's name is. Enjoy!
-Johnny5
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
'IP address input mask written by Johnny5
Dim strIPAddress, numPosition, numOctet
strIPAddress = TextBox1.Text
numPosition = 0
numOctet = 0
If strIPAddress <> "" Then
If Len(strIPAddress) = 1 Then
If IsNumeric(strIPAddress) = False Then
strIPAddress = ""
End If
Else
If IsNumeric(Mid(strIPAddress, Len(strIPAddress), 1)) = False And Mid(strIPAddress, Len(strIPAddress), 1) <> "." Then
strIPAddress = Mid(strIPAddress, 1, Len(strIPAddress) - 1)
Else
If Mid(strIPAddress, Len(strIPAddress), 1) = "." Then
If Mid(strIPAddress, Len(strIPAddress) - 1, 1) = "." Then
strIPAddress = Mid(strIPAddress, 1, Len(strIPAddress) - 1)
End If
numPosition = InStr(strIPAddress, ".") + 1
If InStr(Mid(strIPAddress, numPosition), ".") > 0 Then
numPosition = numPosition + InStr(Mid(strIPAddress, numPosition), ".")
If InStr(Mid(strIPAddress, numPosition), ".") > 0 Then
numPosition = numPosition + InStr(Mid(strIPAddress, numPosition), ".")
If InStr(Mid(strIPAddress, numPosition), ".") > 0 Then
strIPAddress = Mid(strIPAddress, 1, Len(strIPAddress) - 1)
End If
End If
End If
End If
If IsNumeric(Mid(strIPAddress, Len(strIPAddress), 1)) = True Then
If Len(strIPAddress) > 3 Then
If IsNumeric(Mid(strIPAddress, Len(strIPAddress) - 1, 1)) And IsNumeric(Mid(strIPAddress, Len(strIPAddress) - 2, 1)) = True And IsNumeric(Mid(strIPAddress, Len(strIPAddress) - 3, 1)) Then
strIPAddress = strIPAddress.Insert(Len(strIPAddress) - 1, ".")
numPosition = InStr(strIPAddress, ".") + 1
If InStr(Mid(strIPAddress, numPosition), ".") > 0 Then
numPosition = numPosition + InStr(Mid(strIPAddress, numPosition), ".")
If InStr(Mid(strIPAddress, numPosition), ".") > 0 Then
numPosition = numPosition + InStr(Mid(strIPAddress, numPosition), ".")
If InStr(Mid(strIPAddress, numPosition), ".") > 0 Then
strIPAddress = Mid(strIPAddress, 1, Len(strIPAddress) - 2)
End If
End If
End If
End If
If IsNumeric(Mid(strIPAddress, Len(strIPAddress) - 1, 1)) And IsNumeric(Mid(strIPAddress, Len(strIPAddress) - 2, 1)) = True Then
numOctet = FormatNumber(Mid(strIPAddress, Len(strIPAddress) - 2, 1) & Mid(strIPAddress, Len(strIPAddress) - 1, 1) & Mid(strIPAddress, Len(strIPAddress), 1))
If numOctet > 255 Then
strIPAddress = Mid(strIPAddress, 1, Len(strIPAddress) - 3) & "255"
End If
End If
End If
If Len(strIPAddress) = 3 Then
If IsNumeric(strIPAddress) = True Then
numOctet = FormatNumber(Mid(strIPAddress, Len(strIPAddress) - 2, 1) & Mid(strIPAddress, Len(strIPAddress) - 1, 1) & Mid(strIPAddress, Len(strIPAddress), 1))
If numOctet > 255 Then
strIPAddress = Mid(strIPAddress, 1, Len(strIPAddress) - 3) & "255"
End If
End If
End If
End If
End If
End If
TextBox1.Text = strIPAddress
TextBox1.SelectionStart = Len(strIPAddress)
End If
End Sub
au55ie
-Q
Hi John,
Why not just use four MaskedTextBox controls for entry (each with the appropriate numeric mask) and then concatenate them into the IP Address with the periods This is how its done in many MSFT programs for both IP Addresses and Product Keys.
Hope this helps,
Steve Hoag
Visual Basic Express