简体   繁体   中英

VB.NET: Check if input in textbox is in string array

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

This is how i declare my array:

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

I dont seem to do a proper check, help!

The String.Compare method doesn't return a boolean, it returns an integer.

If the strings are equal, it returns 0 .

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

You should set Option Strict to On in your project, so that the compiler won't allow the implicit conversion from boolean to integer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM