hi. i'm have 2 form and a listview in form1 with items, but when the row has no item in it,like row 4 until row 8, an error showed up like this "InvalidArgument=Value of '1' is not valid for 'index'. Parameter name: index". can someone help me fix this? i need to pass the value from listview into labels and if row 4 until 8 is empty, the label will be label4.text = ""

Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click

        Formreceipt.Label1.Text = lvlist2.Items(0).SubItems(1).Text
        Formreceipt.Label2.Text = lvlist2.Items(1).SubItems(1).Text
        Formreceipt.Label3.Text = lvlist2.Items(2).SubItems(1).Text
        Formreceipt.Label4.Text = lvlist2.Items(3).SubItems(1).Text
        Formreceipt.Label5.Text = lvlist2.Items(4).SubItems(1).Text
        Formreceipt.Label6.Text = lvlist2.Items(5).SubItems(1).Text
        Formreceipt.Label7.Text = lvlist2.Items(6).SubItems(1).Text
        Formreceipt.Label8.Text = lvlist2.Items(7).SubItems(1).Text

        Formreceipt.Show()
    End Sub

You can change every assignement by using the IIF(test, true_part, false_part) function:

Formreceipt.Label1.Text = IIF(lvlist2.Items.Count > 0, lvlist2.Items(0).SubItems(1).Text, "")
Formreceipt.Label1.Text = IIF(lvlist2.Items.Count > 1, lvlist2.Items(1).SubItems(1).Text, "")
Formreceipt.Label1.Text = IIF(lvlist2.Items.Count > 2, lvlist2.Items(2).SubItems(1).Text, "")
'
'    etc.
'
.
.

Hope this helps

still error

i think error at

IIF(boolean, truepart , falsepart)

help please.

Can you be so kind to show us the current code?

1

actually i'm totally clueless. i'm newbies. sorry for that.

i solved it, although need some effort,i change the listview items with "0" so that the index of item is not null. i use the code but this is to long.
a bit relief when its done, but for knowledge, can someone give the shortest way?

Dim li0 As New ListViewItem
        
        Dim ass As Integer


        If lvlist2.Items.Count = 0 Then

            For ass = 0 To 7
                li0 = lvlist2.Items.Add("0")
                li0.SubItems.Add("0")
                li0.SubItems.Add("0")
                li0.SubItems.Add("0")
                li0.SubItems.Add("0")
                li0.SubItems.Add("0")
                li0.SubItems.Add("0")
            Next ass

            Formreceipt.KryptonLabel1.Text = lvlist2.Items(0).SubItems(1).Text
            Formreceipt.KryptonLabel2.Text = lvlist2.Items(1).SubItems(1).Text
'etc

 
 If lvlist2.Items.Count = 1 Then

            For ass = 0 To 7
                li0 = lvlist2.Items.Add("0")
                li0.SubItems.Add("0")
                li0.SubItems.Add("0")
                li0.SubItems.Add("0")
         'etc

but this is to long.
a bit relief when its done, but for knowledge, can someone give the shortest way?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.