这有什么问题? (我搜索了几个小时的错误)

时间:2018-05-24 19:08:41

标签: vb.net

我有这段代码

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim Location1 As String
    Dim city As Boolean
    Dim sss As Boolean
    If Not IsNumeric(TextBox1.Text) Then
        MsgBox("Invalid ip address")
    Else
        city = True
        sss = False
        geoip()
        If Label1.Text = "" Or Label1.Text = " " Then
            city = False
        Else
            city = True
            Location1 = Label1.Text
        End If
        If city = False And Label16.Text = "" Or Label16.Text = " " Then
            Location1 = Label17.Text
        Else
            sss = True
        End If
        If sss = True Then
            Location1 = Label16.Text
        End If
    End If
    TextBox1.Text = ""
    If CheckBox2.Checked = True Then
        TextBox2.Text = "We look forward to meet you tomorrow at the meeting place at 1 PM, the package has been shipped already to you to " + Location1 + ". If you wont be there for completing the transaction, we will need to proceed to stage 2."
    End If
    If CheckBox1.Checked = True Then
        TextBox2.Text = "My name is " + TextBox3.Text + " from " + TextBox4.Text + " and im here for letting you know that we look forward to meet you tomorrow at the meeting place at 1 PM, the package has been shipped already to you to " + Location1 + ". If you wont be there for completing the transaction, we will need to proceed to stage 2."
    End If
    TextBox1.Text = ""
End Sub

它没有按预期工作。它甚至在label1不是""" "作为标签16时显示,我在代码中搜索了很长时间的错误,但我认为一切都很好

3 个答案:

答案 0 :(得分:1)

我现在明白你的问题了 - 我要留下我的旧答案,因为括号条件是正确的,你的代码仍然会破坏,但你的问题是你有If ... else这样你就属于在您不想要的地方else

例如 - 在代码中考虑这一行:

If city = False And Label16.Text = "" Or Label16.Text = " " Then
    Location1 = Label17.Text
Else
    sss = True
End If

如果城市为真,或者标签16中有某些内容,则else会被点击。您只希望它到达那里如果城市仍然是假的并且有一些东西在标签16 ....

而是专注于通过嵌套条件逻辑来完成逻辑。此代码应按预期工作:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim Location1 As String
    Dim city As Boolean
    Dim sss As Boolean
    If Not IsNumeric(TextBox1.Text) Then
        MsgBox("Invalid ip address")
    Else
        geoip()

        If Label1.Text.Trim() = "" Then
            city = False

            If Label16.Text.Trim() = "" Then
                Location1 = Label17.Text
            Else
                sss = True
                Location1 = Label16.Text
            End If
        Else
            city = True
            Location1 = Label1.Text
        End If
    End If
    TextBox1.Text = ""
    If CheckBox2.Checked = True Then
        TextBox2.Text = "We look forward to meet you tomorrow at the meeting place at 1 PM, the package has been shipped already to you to " + Location1 + ". If you wont be there for completing the transaction, we will need to proceed to stage 2."
    End If
    If CheckBox1.Checked = True Then
        TextBox2.Text = "My name is " + TextBox3.Text + " from " + TextBox4.Text + " and im here for letting you know that we look forward to meet you tomorrow at the meeting place at 1 PM, the package has been shipped already to you to " + Location1 + ". If you wont be there for completing the transaction, we will need to proceed to stage 2."
    End If
    TextBox1.Text = ""
End Sub

答案 1 :(得分:0)

我不理解你所说的问题,但我很确定你的问题与你的条款包围有关。

您的第If city = False And Label16.Text = "" Or Label16.Text = " " Then行正确读给您,但编译器无法正确理解...始终将复杂逻辑语句括起来以保证安全。

尝试并更改此内容:

If city = False And Label16.Text = "" Or Label16.Text = " " Then

到此:

If city = False And (Label16.Text = "" Or Label16.Text = " ") Then

或者,请更好地解释问题......

答案 2 :(得分:0)

我认为您使用的IsNumeric函数不能用于IP地址。尝试拆分ip,然后查看IsNumeric