If Like语句没有输出?

时间:2014-03-26 20:33:38

标签: vb.net

所以我试图让这个代码工作,但它不返回If Like部分的任何值 的代码。我不知道为什么会这样。

Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
    'display color button click event procedure should display the color of the item
    'whos item number is entered by the user.

    'all item numbers contain exactly seven characters
    'all items are available in four colors: blue, green, red, and white.
    'the fourth character in the item number indicates the items color
    'as follows: B or b indicates blue etc
    'if the item number does not contain 7 charactors OR
    'if the forth character is not one of the valid color characters,
    'the procedure should display the appropriate message

    If txtItem.Text Like "###[bBgGrRrwW]###" Then
        If txtItem.Text.Contains("bB") Then
            lblColor.Text = "Blue"
        ElseIf txtItem.Text.Contains("gG") Then
            lblColor.Text = "Green"
        ElseIf txtItem.Text.Contains("rR") Then
            lblColor.Text = "Red"
        ElseIf txtItem.Text.Contains("Ww") Then
            lblColor.Text = "White"
        End If
    ElseIf txtItem.Text IsNot "###[bBgGrRwW]###" Then
        MessageBox.Show("Bad Job", "Color Project", MessageBoxButtons.OKCancel,
                        MessageBoxIcon.Information)
    End If
End Sub

1 个答案:

答案 0 :(得分:4)

如果您有.Contains("bB"),则要求它检查字符串是否包含bB,即两个字符。您实际想要做的是检查它是否包含B并忽略该字符的大小写。虽然Contains没有不区分大小写的选项,IndexOf会执行:String.IndexOf Method (String, StringComparison),所以您可以使用

If txtItem.Text.IndexOf("B", StringComparison.OrdinalIgnoreCase) >= 0 Then