Question Check out if a value exists on the listview

theawesomerb

Member
Joined
Sep 29, 2012
Messages
6
Programming Experience
Beginner
Hello,
in my form i got one textbox , one listview and one button

i want that when i click the button1 ,the program check out if there are already an item in the listview with the textbox1 value (a file path , so its string)

here is my code :

VB.NET:
                       For Each item As ListViewItem In ListView1.Items
                If item.ListView.Items(0).Text = Textbox1.Text Then
                    MsgBox("the file exists in the listview")
                    Return
                ElseIf item.ListView.Items(0).Text <> Textbox1.Text Then
                    MsgBox("the file DOESN'T exists in the listview")
                    Return
                End If
            Next


the problem is , i think the execution of this code isn't really smooth , and it check only for the first item , not on the others , i want to make it check if that value exists an all items in the listview (but not in the sub-items) .

thanks you very much , please be quick if you can .
 
Hi,

You have over complicated your code. See my comments below:-

VB.NET:
'this statement is fine
For Each item As ListViewItem In ListView1.Items
  'this statement is incorrect since item already exposes the listviewitem
  'properties
  If item.ListView.Items(0).Text = Textbox1.Text Then
    'this is fine
    MsgBox("the file exists in the listview")
    'return has no relevance here and is only used in functions to return a
    'value / object to the calling routine
    Return
  'elseif has no relevance here since this is used to test for multiple
  'conditions within the same if statement. Since you have only one real
  'conditional test above a simple else statement will be triggered if the
  'tested condition above returns false. i.e. NOT = Textbox1.Text
  elseIf item.ListView.Items(0).Text <> Textbox1.Text Then
    'this is fine
    MsgBox("the file DOESN'T exists in the listview")
    'see the same comment above
    Return
  End If
Next
Overall the reason why your code does not work for the whole ListView is that regardless of the condition of the first listview item that is tested you then make a call to Return which then terminates the execution of the code.

This is how you should have written your code.

VB.NET:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim FileFound As Boolean
 
    For Each item As ListViewItem In ListView1.Items
      'the correct way to use item
      If item.Text = TextBox1.Text Then
        MsgBox("the file exists in the listview")
        'If the file is found then no need to continue with the loop
        'Set file found to true and drop out of the for loop
        FileFound = True
        Exit For
        'notice that we have no else statement here.
        'That's because we want to keep iterating through the listview
        'until we get to the end of the listview
      End If
    Next
    'this bit is new to test for the file not being found
    'As you can see above if the file is found then FileFound will
    'be set to true but if it was NOT set to true in the for loop
    'above then when you get here FileFound will be false and so
    'you can test for the file not being found here and deal
    'with it.
    If Not FileFound Then
      MsgBox("the file DOESN'T exists in the listview")
    End If
  End Sub
The only other thing you may want to consider here is case sensitivity. if you want to get round case sensitivity then you could use If item.Text.ToLower = TextBox1.Text.ToLower in your If statement above.

Hope this helps and good luck.

Cheers,

Ian
 
Last edited:
Worked like charm ! , thanks ! you didn't just gave me the solution , you showed me my errors , thanks dude , you're a half man , half amazing ! :p
 
Back
Top