none
Compare textbox input with Listbox existing input Visual Basic VS2010 RRS feed

  • Question

  • I am new to visual basic and I am stuck in my code where I try to compare the input in my textbox with the existing items in my Listbox. I used the code below but as I  click "Check", I am getting error on my iteration. The total inputs in the listbox is indefinite and depends on the user input. I hope someone can advise me. Thank you.

    Below is my code for comparing for the textbox and listbox entry under Check button

    Dim i = 0        
    Dim count As Integer = ListBox1.Items.Count - 1       
    Dim entry As String = ListBox1.Items.Item(i)        
    Dim scan As String = SerialTxt.Text        
    Dim SelevenNo As String = Mid(scan, 12, 5)        
    Dim EelevenNo As String = Mid(entry, 12, 5)        
    
    If Len(SerialTxt.Text) < 16 Then            
    MsgBox("Serial Number must be 16 characters long!", vbExclamation, "Error!")            
    SerialTxt.Text = ""            
    Exit Sub        
    End If        
    
    If SerialTxt.Text = "" Then            
    
    MsgBox("Enter some data")            
    
    Exit Sub        
    End If        
    
    ListBox1.Items.Add(scan)        
    
    If CLng(QtyRemaining.Text) = 0 Then            
    MsgBox("You have exceeded the sampling quantity!", vbExclamation, "Error!!!")            
    Exit Sub        
    End If                    
    If count > 1 Then            
    If StrComp(Mid(scan, 1, 11), (Mid(entry, 1, 11)), vbTextCompare) Then                ListBox1.Items.Add(scan)                
    MsgBox("Equal!!")           
    End If
    End If
    End Sub


    Thursday, March 21, 2019 5:03 AM

Answers

  • Hi,

    Your method becomes StrComp again, and the StrComp function returns the following value:

    -1( string1 < string2)
    0 ( string1 = string2)
    1 ( string1 > string2)

    so you should fix your code:

      If StrComp((Mid(scan, 1, 11)), CStr(Mid(entry, 1, 11)), vbTextCompare) <> 0 Then
    
                        MsgBox("Not Equal")
    
                    End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by txrxmo90 Monday, March 25, 2019 3:05 AM
    Friday, March 22, 2019 5:22 AM
  • try the code:

    If StrComp((Mid(scan, 1, 11)), CStr(Mid(entry, 1, 11)), vbTextCompare) <> 0 Then
    
                        MsgBox("Not Equal")
                        Exit Sub
                    End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by txrxmo90 Monday, March 25, 2019 3:04 AM
    Monday, March 25, 2019 1:22 AM

All replies

  • Hi,

    fix the code:

      If count > 1 Then
                If StrComp(Mid(scan, 1, 11), (Mid(entry, 1, 11)), vbTextCompare) = 0 Then
                    ListBox1.Items.Add(scan)
                    MsgBox("Equal!!")
                End If
    
            End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, March 21, 2019 5:57 AM
  • Hi Thanks for the reply! 

    I manage to fix it already to :

    Dim i As Integer
            If count > 0 Then
    
                For i = 0 To count - 1
                    entry = ListBox1.Items.Item(i)
                    'For i = 0 To count
                    If InStr((Mid(scan, 1, 11)), (Mid(entry, 1, 11)), vbTextCompare) = 0 Then
                        ListBox1.Items.Add(scan)
                        MsgBox("Equal!!")
                    End If

    But the comparison does not seem to work. Am I using the comparison correctly? I am hoping to compare the first 11 characters of the 2 inputs and if not the same, it should will take in the input from the textbox to Listbox.

    Thank you



    • Edited by txrxmo90 Thursday, March 21, 2019 7:06 AM
    Thursday, March 21, 2019 7:00 AM
  • Hi Thanks for the reply! 

    I manage to fix it already to :

    Dim i As Integer
            If count > 0 Then
    
                For i = 0 To count - 1
                    entry = ListBox1.Items.Item(i)
                    'For i = 0 To count
                    If InStr((Mid(scan, 1, 11)), (Mid(entry, 1, 11)), vbTextCompare) = 0 Then
                        ListBox1.Items.Add(scan)
                        MsgBox("Equal!!")
                    End If

    But the comparison does not seem to work. Am I using the comparison correctly?

    Thank you


    Hi,

    You use Instr method,InStr(a, b) , if a contains b, InStr(a, b) returns a value greater than 0, or 0 if it does not contain b,try to fix 

    InStr((Mid(scan, 1, 11)), (Mid(entry, 1, 11)), vbTextCompare) > 0 

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Thursday, March 21, 2019 7:15 AM
  • Hi Thank you! it works on the comparison! But even when the values are not equal, it continues to print.

    Is there a way to prevent the textbox input from printing in the listbox if it is not equal?

    I tried to do an If Else but it still prints. 
    • Edited by txrxmo90 Thursday, March 21, 2019 8:33 AM
    Thursday, March 21, 2019 8:32 AM
  • Hi,

    Here is your current code?What is your loop used for?Please provide your complete code.

    Public Class Form1
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    
            Dim count As Integer = ListBox1.Items.Count - 1
            Dim entry As String
    
            Dim scan As String = SerialTxt.Text
            Dim SelevenNo As String = Mid(scan, 12, 5)
    
            If Len(SerialTxt.Text) < 16 Then
                MsgBox("Serial Number must be 16 characters long!", vbExclamation, "Error!")
                SerialTxt.Text = ""
                Exit Sub
            End If
    
            If SerialTxt.Text = "" Then
    
                MsgBox("Enter some data")
    
                Exit Sub
            End If
    
            ListBox1.Items.Add(scan)
    
            If CLng(QtyRemaining.Text) = 0 Then
                MsgBox("You have exceeded the sampling quantity!", vbExclamation, "Error!!!")
                Exit Sub
            End If
            'MsgBox(StrComp(Mid(scan, 1, 11), (Mid(entry, 1, 11)), vbTextCompare))
            'If count > 1 Then
            '    If StrComp(Mid(scan, 1, 11), (Mid(entry, 1, 11)), vbTextCompare) Then
            '        ListBox1.Items.Add(scan)
            '        MsgBox("Equal!!")
            '    End If
    
            'End If
            Dim i As Integer
            If count > 0 Then
    
                For i = 0 To count - 1
    
    
                    entry = ListBox1.Items.Item(i)
                    'For i = 0 To count
                    If InStr((Mid(scan, 1, 11)), (Mid(entry, 1, 11)), vbTextCompare) > 0 Then
                        ListBox1.Items.Add(scan)
                        MsgBox("Equal!!")
                    End If
                Next
            End If
    
    
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
        End Sub
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, March 21, 2019 9:11 AM
  • The loop is for the new input in the listbox. This buttonClick is part of a program that compares an indefinite qty of 16 alphanumeric characters input to the listbox based on the sample size. Thank you so much for the help and advise! :)

    Actually this is my full code:

    Public Class Form1 Private Sub Submit_Click(sender As Object, e As EventArgs) Handles Submit.Click UnitListBox.Show() If LotQty.Text = "" Then MsgBox("Please fill in the Lot Qty!", vbExclamation, "Error!") vbExclamation, "Error!!!") Exit Sub End If QtyRequired.Text = FuncQty.Text QtyRemaining.Text = FuncQty.Text End Sub Private Sub FunctionalBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles FunctionalBox1.SelectedIndexChanged If LotQty.Text = "" Then Exit Sub Select Case FunctionalBox1.SelectedItem Case "100%" FuncQty.Text = LotQty.Text Case "50%" If (LotQty.Text) = 1 Then FuncQty.Text = "1" Else FuncQty.Text = Math.Round(CLng(LotQty.Text) * 0.5, 0) End If Case "20%" If CLng(LotQty.Text) = 1 Or CLng(LotQty.Text) = 2 Then FuncQty.Text = "1" Else FuncQty.Text = Math.Round(CLng(LotQty.Text) * 0.2, 0) End If Case "N=5" If CLng(LotQty.Text) > 5 Then MsgBox("Lot Qty must be 5!", vbExclamation, "Error!") Else FuncQty.Text = 5 End If Case "AQL 0.4" Select Case CLng(LotQty.Text) Case 1 To 25 FuncQty.Text = LotQty.Text Case 26 To 280 FuncQty.Text = "32" Case 281 To 500 FuncQty.Text = "48" Case 501 To 3200 FuncQty.Text = "73" Case 3201 To 10000 FuncQty.Text = "86" Case 10001 To 35000 FuncQty.Text = "108" Case 35001 To 150000 FuncQty.Text = "123" Case 150001 To 500000 FuncQty.Text = "156" Case Is > 500000 FuncQty.Text = "189" End Select End Select End Sub Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load FunctionalBox1.Enabled = False FunctionalBox1.SelectedIndex = "4" VisualBox.Enabled = False VisualBox.SelectedIndex = "4" UnitListBox.Hide() End Sub Private Sub EditFBtn_Click(sender As Object, e As EventArgs) Handles EditFBtn.Click FunctionalBox1.Enabled = True If String.IsNullOrEmpty(LotQty.Text) Then MsgBox("Please fill in the Lot Qty!", vbExclamation, "Error!") End If End Sub Private Sub EditVBtn_Click(sender As Object, e As EventArgs) Handles EditVBtn.Click VisualBox.Enabled = True If String.IsNullOrEmpty(LotQty.Text) Then MsgBox("Please fill in the Lot Qty!", vbExclamation, "Error!") End If End Sub Private Sub VisualBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles VisualBox.SelectedIndexChanged If LotQty.Text = "" Then Exit Sub Select Case VisualBox.SelectedItem Case "100%" VisualQty.Text = LotQty.Text Case "50%" If (LotQty.Text) = 1 Then VisualQty.Text = "1" Else VisualQty.Text = Math.Round(CLng(LotQty.Text) * 0.5, 0) End If Case "20%" If CLng(LotQty.Text) = 1 Or CLng(LotQty.Text) = 2 Then VisualQty.Text = "1" Else VisualQty.Text = Math.Round(CLng(LotQty.Text) * 0.2, 0) End If Case "N=5" If CLng(LotQty.Text) > 5 Then MsgBox("Lot Qty must be 5!", vbExclamation, "Error!") Else VisualQty.Text = 5 End If Case "AQL 0.4" Select Case CLng(LotQty.Text) Case 1 To 25 VisualQty.Text = LotQty.Text Case 26 To 280 VisualQty.Text = "32" Case 281 To 500 VisualQty.Text = "48" Case 501 To 3200 VisualQty.Text = "73" Case 3201 To 10000 VisualQty.Text = "86" Case 10001 To 35000 VisualQty.Text = "108" Case 35001 To 150000 VisualQty.Text = "123" Case 150001 To 500000 VisualQty.Text = "156" Case Is > 500000 VisualQty.Text = "189" End Select End Select End Sub Private Sub Reset_Click(sender As Object, e As EventArgs) Handles Reset.Click VisualQty.Clear() FuncQty.Clear() LotQty.Clear() FunctionalBox1.Enabled = False FunctionalBox1.SelectedIndex = 4 VisualBox.Enabled = False VisualBox.SelectedIndex = 4 End Sub Private Sub LotQty_TextChanged(sender As Object, e As EventArgs) Handles LotQty.TextChanged If LotQty.Text = "" Then FuncQty.Text = "" VisualQty.Text = "" QtyRemaining.Text = "" QtyRequired.Text = "" Exit Sub End If Select Case FunctionalBox1.SelectedItem Case "100%" FuncQty.Text = LotQty.Text 'Me.txtFunctional.Enabled = False Case "50%" If (LotQty.Text) = 1 Then FuncQty.Text = "1" Else FuncQty.Text = Math.Round(CLng(LotQty.Text) * 0.5, 0) End If Case "20%" If CLng(LotQty.Text) = 1 Or CLng(LotQty.Text) = 2 Then FuncQty.Text = "1" Else FuncQty.Text = Math.Round(CLng(LotQty.Text) * 0.2, 0) End If Case "N=5" If CLng(LotQty.Text) > 5 Then MsgBox("Lot Qty must be 5!", vbExclamation, "Error!") Else FuncQty.Text = 5 End If Case "AQL 0.4" Select Case CLng(LotQty.Text) Case 1 To 25 FuncQty.Text = LotQty.Text Case 26 To 280 FuncQty.Text = "32" Case 281 To 500 FuncQty.Text = "48" Case 501 To 3200 FuncQty.Text = "73" Case 3201 To 10000 FuncQty.Text = "86" Case 10001 To 35000 FuncQty.Text = "108" Case 35001 To 150000 FuncQty.Text = "123" Case 150001 To 500000 FuncQty.Text = "156" Case Is > 500000 FuncQty.Text = "189" End Select End Select Select Case VisualBox.SelectedItem Case "100%" VisualQty.Text = LotQty.Text Case "50%" If (LotQty.Text) = 1 Then VisualQty.Text = "1" Else VisualQty.Text = Math.Round(CLng(LotQty.Text) * 0.5, 0) End If Case "20%" If CLng(LotQty.Text) = 1 Or CLng(LotQty.Text) = 2 Then VisualQty.Text = "1" Else VisualQty.Text = Math.Round(CLng(LotQty.Text) * 0.2, 0) End If Case "N=5" If CLng(LotQty.Text) > 5 Then MsgBox("Lot Qty must be 5!", vbExclamation, "Error!") Else VisualQty.Text = 5 End If Case "AQL 0.4" Select Case CLng(LotQty.Text) Case 1 To 25 VisualQty.Text = LotQty.Text Case 26 To 280 VisualQty.Text = "32" Case 281 To 500 VisualQty.Text = "48" Case 501 To 3200 VisualQty.Text = "73" Case 3201 To 10000 VisualQty.Text = "86" Case 10001 To 35000 VisualQty.Text = "108" Case 35001 To 150000 VisualQty.Text = "123" Case 150001 To 500000 VisualQty.Text = "156" Case Is > 500000 VisualQty.Text = "189" End Select End Select End Sub Private Sub CheckBtn_Click(sender As Object, e As EventArgs) Handles CheckBtn.Click ' Dim i As Integer Dim Sequence As Long Dim Unit As String Dim count As Integer = ListBox1.Items.Count Dim entry Dim scan As String = SerialTxt.Text Dim compareSno As Integer 'Dim SelevenNo As String = Mid(scan, 12, 5) 'Dim EelevenNo As String = Mid(entry, 12, 5) If Len(SerialTxt.Text) < 16 Then MsgBox("Serial Number must be 16 characters long!", vbExclamation, "Error!") SerialTxt.Text = "" Exit Sub End If If SerialTxt.Text = "" Then MsgBox("Enter some data") Exit Sub End If ListBox1.SelectedIndex = -1 SerialTxt.Focus() If CLng(QtyRemaining.Text) = 0 Then MsgBox("You have exceeded the sampling quantity!", vbExclamation, "Error!!!") Exit Sub End If Sequence = 0 Unit = "" 'SerialTxt.Text = ListBox1.SelectedItem.ToString Dim i As Integer If count > 0 Then For i = 0 To count - 1 entry = ListBox1.Items.Item(i) 'For i = 0 To count If StrComp((Mid(scan, 1, 11)), CStr(Mid(entry, 1, 11)), vbTextCompare) > 0 Then MsgBox("Not Equal") End If Next End If ListBox1.Items.Add(scan) QtyRemaining.Text = QtyRequired.Text - 1 scan = ""



    • Edited by txrxmo90 Thursday, March 21, 2019 10:24 AM
    Thursday, March 21, 2019 10:20 AM
  • Hi,

    Your method becomes StrComp again, and the StrComp function returns the following value:

    -1( string1 < string2)
    0 ( string1 = string2)
    1 ( string1 > string2)

    so you should fix your code:

      If StrComp((Mid(scan, 1, 11)), CStr(Mid(entry, 1, 11)), vbTextCompare) <> 0 Then
    
                        MsgBox("Not Equal")
    
                    End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by txrxmo90 Monday, March 25, 2019 3:05 AM
    Friday, March 22, 2019 5:22 AM
  • Thanks so much for the help @Alex. But is there a way i can prevent the input from printing in the listbox if the S/N dont match?

    Monday, March 25, 2019 1:11 AM
  • try the code:

    If StrComp((Mid(scan, 1, 11)), CStr(Mid(entry, 1, 11)), vbTextCompare) <> 0 Then
    
                        MsgBox("Not Equal")
                        Exit Sub
                    End If

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by txrxmo90 Monday, March 25, 2019 3:04 AM
    Monday, March 25, 2019 1:22 AM
  • Oh wow! Thanks alot! It worked perfectly!
    Monday, March 25, 2019 3:04 AM