Matthew
Matthew

Reputation: 640

listview prompting user the item is empty in vb.net

I have a list that has 2 records, and also I have a button to prompt the user that the 3rd record is empty.

Here is my code:

If lvFabric2.Items(2).Text Is Nothing Then
   MsgBox("The 3rd record is empty")
end if

I always get this error: "InvalidArgument=Value of '2' is not valid for 'index'. Parameter name: index"

How can I prompt the user that the 3rd record is empty.

Thank you

Upvotes: 0

Views: 816

Answers (2)

htxryan
htxryan

Reputation: 2961

Ironically, you're getting the error because the third item is null.

Try this instead:

If lvFabric2.Items.Count() < 3 OrElse lvFabric2.Items(2).Text Is Nothing Then
   MsgBox("The 3rd record is empty")
end if

This will show your message box if there is no third element, or if the third element is null.

Note that I'm not 100% sure of the syntax. "Count" may be a property, so you may need "Items.Count" instead.

EDIT: Fixed syntax from C# ("||") to VB ("OrElse")

Upvotes: 2

rwisch45
rwisch45

Reputation: 3702

Try If lvFabric2.Items(2).ToString().length = 0 Then

Upvotes: 0

Related Questions