首页 > 解决方案 > 这有什么问题?(我搜索了几个小时的错误)

问题描述

我有这个代码

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 也会显示,我在代码中搜索了很长时间的错误,但我认为一切都很好

标签: vb.net

解决方案


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

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

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

如果 city 为 true 或者标签 16 中包含某些内容,则会受到影响。如果 city 仍然为 false并且标签 16 中有else某些内容,您只会希望它到达那里。

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

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

推荐阅读