Click here to Skip to main content
15,116,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my window application i have list view with 4 column.i want to check 3rd sub item is empty or not in all the rows.if it was empty i need to prompt an error.how to check sub-item 3 doesn't have any values?
Posted
Comments
Richard MacCutchan 20-Jun-13 3:29am
   
Read the items and test them; how else?

1 solution

VB
If myListView.Items(17).SubItems(3) Is Nothing Or myListView.Items(17).SubItems(3).Text <> "" Then


Or if you need to check all:

VB
Dim emptyFieldExists As Boolean = False

For Each item As ListViewItem In myListView.Items
    If item.SubItems(3) Is Nothing Or item.SubItems(3).Text <> "" Then
        emptyFieldExists = True
    End If
Next

If emptyFieldExists Then
    'Do what you need to do!
End If
   
v5
Comments
jai_mca 21-Jun-13 5:34am
   
when the flow reaches if item.SubItems(3) condition it prompt the error like the below


InvalidArgument=Value of '3' is not valid for 'index'.
Parameter name: index
Johnny J. 21-Jun-13 5:42am
   
That's why you need to check is the item is nothing before checking the text property. If my code above still fails, try nesting the conditions:

Dim emptyFieldExists As Boolean = False

For Each item As ListViewItem In myListView.Items
If Not item.SubItems(3) Is Nothing Then
If item.SubItems(3).Text <> "" Then
emptyFieldExists = True
End If
Else
emptyFieldExists = True
End If
Next

If emptyFieldExists Then
'Do what you need to do!
End If
jai_mca 21-Jun-13 7:49am
   
thanks johnny..
but now also it prompt the same error,
when it reaches item.subitem[3].text
Johnny J. 22-Jun-13 9:38am
   
Try this:

Dim emptyFieldExists As Boolean = False

For Each item As ListViewItem In myListView.Items
If item.SubItems.Count > 4 Then
If Not item.SubItems(3) Is Nothing And item.SubItems(3).Text <> "" Then
emptyFieldExists = True
End If
Else
emptyFieldExists = True
End If
Next

If emptyFieldExists Then
'Do what you need to do!
MsgBox("Empty items")
Else
MsgBox("No Empty items")
End If

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