VB.NET:检查文本框中的输入是否在字符串数组中

时间:2012-09-26 10:15:08

标签: vb.net

Private Sub txtQty_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQty.GotFocus
   Dim strItem As String
   strItem = txtItem.Text
   Dim strArray As String
   strArray = itemArr(1)

   If String.Compare(strItem, strArray) = True Then
       MessageBox.Show("item in array!")
   End If
End Sub

Private Sub txtQty_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtQty.TextChanged

    If txtItem.Text <> Nothing And txtQty.Text <> Nothing Then

        'create rows in the DataTable
        tblScItem.Rows.Add(itemArray())
    End If

    txtItem.Text = ""
    txtQty.Text = ""

End Sub

这就是我声明我的数组的方式:

Function itemArray() As String()

        itemArr(0) = ""
        itemArr(1) = txtItem.Text
        itemArr(2) = Form2.cbGondola.SelectedItem
        itemArr(3) = txtQty.Text
        itemArr(4) = DateTime.Now
        itemArr(5) = Form1.txtLoginId.Text

        Return itemArr
End Function

我似乎没有做适当的检查,帮助!

1 个答案:

答案 0 :(得分:1)

String.Compare method不返回布尔值,它返回一个整数。

如果字符串相等,则返回0

If String.Compare(strItem, strArray) = 0 Then

您应该在项目中将Option Strict设置为On,以便编译器不允许从布尔值到整数的隐式转换。