0

我正在尝试输入 IP 1 textbox,当我按 OK 时,必须分成 4 部分,例如:

123.123.123.123

并拆分为textbox1,2,3,4[123] [123] [123] [123]一切都可以,但如果我输入更多数字 123.123.123.123.123.123.123 我会出错。

    Dim str As String = TextBox1.Text
    Dim splitStr As String() = str.Split(".")
    TextBox1.Text = splitStr(0).ToString()
    TextBox2.Text = splitStr(1).ToString()
    TextBox3.Text = splitStr(2).ToString()
    TextBox4.Text = splitStr(3).ToString()
4

2 回答 2

2

这涵盖了大多数输入错误:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""

    Dim str As String = TextBox1.Text
    str = str.Trim
    Dim splitStrs As String() = str.Split("."c)

    If splitStrs.Length <> 4 Then
        MsgBox("Not a valid IP format")
        Exit Sub
    End If

    For Each value As String In splitStrs
        If value.Length < 1 OrElse value.Length > 3 Then
            MsgBox("Not a valid IP format")
            Exit Sub
        End If


        If Not Integer.TryParse(value, 0) Then
            MsgBox("Not a valid IP format")
            Exit Sub
        End If

        If value.Contains(" ") Then
            MsgBox("Not a valid IP format")
            Exit Sub
        End If
    Next
    TextBox1.Text = splitStrs(0).ToString()
    TextBox2.Text = splitStrs(1).ToString()
    TextBox3.Text = splitStrs(2).ToString()
    TextBox4.Text = splitStrs(3).ToString()
End Sub
于 2013-02-20T23:11:09.150 回答
0

将字符串的长度限制为 15 个字符

4 x 3 个字符“000”加上 3 个分隔符“.”

共 15 个字符

于 2013-02-20T19:02:59.120 回答