Author Topic: Enter IP Address into Masked Edit Control  (Read 1556 times)

Darrell

  • Full Member
  • ***
  • Posts: 198
  • Electrician / PLC Technician
    • View Profile
Enter IP Address into Masked Edit Control
« on: October 30, 2014, 03:10:07 PM »
Trying to get this to work , any help would be appreciated.


Want to enter IP Address into Masked Edit Control, check if entry is valid (0-255) and jump to the next section of the mask using the dot (.) as separator, if the section is shorter than 3 digits (such as : 192.168.24.1).


Private Sub MaskedTextBox1_MaskInputRejected(sender As System.Object, e As System.Windows.Forms.MaskInputRejectedEventArgs) Handles MaskedTextBox1.MaskInputRejected


        Dim myIPValue As String


    End Sub
    Private Sub Form_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.SelStart = 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.SelStart = (v - 1) * 4
                    Exit For
                End If
            End If
        Next (v)
        myIPValue = Replace(MaskedTextBox1.Text, " ", "")
    End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles ChangeIPButton.Click
        EthernetIPforPLCSLCMicroCom1.IPAddress = MaskedTextBox1.Text
    End Sub

« Last Edit: October 30, 2014, 04:50:41 PM by jazzman448899 »

Archie

  • Administrator
  • Hero Member
  • *****
  • Posts: 5322
    • View Profile
    • AdvancedHMI
Re: Enter IP Address into Masked Edit Control
« Reply #1 on: October 30, 2014, 09:12:16 PM »
Add the attached to your project to see if it gets you what you need