Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have i list view with drive info
i create a button with timer to refresh the list view
i want to create an if that if list view have something do nothing and if list view losing something close the application

VB
Dim _itm As ListViewItem
 For Each _itm In ListView1.Items
                If (_itm.SubItems(0).Text).ToString.Equals("") Then
                    Exit For
                End If
                If Not (_itm.SubItems(0).Text).ToString.Equals("") Then
                    Me.Close()
                End If
            Next


What I have tried:

VB
Dim _itm As ListViewItem
 For Each _itm In ListView1.Items
                If (_itm.SubItems(0).Text).ToString.Equals("") Then
                    Exit For
                End If
                If Not (_itm.SubItems(0).Text).ToString.Equals("") Then
                    Me.Close()
                End If
            Next
Posted
Updated 16-Apr-16 3:14am
Comments
Michael_Davies 16-Apr-16 6:47am    
Not quite sure what you are asking but what you have will not work as the second IF will not execute as the first (same condition) IF will exit the loop....

Try:

If (_itm.SubItems(0).Text).ToString.Equals("") Then
Me.Close()
Exit For
End If
Stm21 16-Apr-16 6:59am    
work with that and thank you so much but i want the opposite
if the list view losing specify text the program close
Michael_Davies 16-Apr-16 8:16am    
Apologise, put the NOT condition first before the Exit For condition. Or better add an else:

If (_itm.SubItems(0).Text).ToString.Equals("") Then
Exit For
Else
Me.Close()
End If

Stm21 16-Apr-16 13:01pm    
Mr Michael_Davies with the last code when i run the application the application close after 1 second with the text equal.
I think i did't do anything wrong.
Stm21 16-Apr-16 13:21pm    
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click, Timer1.Tick
ListView1.Items.Clear()
Dim i As Integer = 0
For Each drive As IO.DriveInfo In IO.DriveInfo.GetDrives
Dim itemText As String = drive.Name
Dim Type As String
Dim ltr As String = drive.Name
If drive.IsReady AndAlso drive.VolumeLabel <> "" Then
itemText = drive.VolumeLabel
End If
ListView1.Items.Add(itemText)
Dim _itm As ListViewItem
'For Each _itm In ListView1.Items
'If Not (_itm.SubItems(0).Text).ToString.Equals("some text") Then
'Me.Close()
'End If
'Next
Next

End Sub


please what i do wrong???
pleaseeeeeeee help

1 solution

You're going about this wrong. You're treating the ListView as a container of your application data. DON'T get into this very bad habit.

UI controls are there to show a visual representation of the data model your application holds. They should never be used AS the data model.

Don't check a ListView for data. Check the data you hold instead for the item you're looking for.

By doing this, you separate your data model from the UI making it possible to use more of your code independent from the type of application you're writing such as Windows Forms, WPF, ASP.NET, Mobile, ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900