hello, ahm, can somebody help me, i'm always getting this error

InvalidArgument=Value of '25' is not valid for 'index'.
Parameter name: index

this is my code:

  Dim currrentRow As Integer
        currrentRow = lv2T1.Items.Count()

        If compare(lv1T1.Items(currrentRow).SubItems(2).Text, txt2T1.Text) = True Then

            Dim lv As ListViewItem
            lv = lv2T1.Items.Add(txt1T1.Text)
            lv.SubItems.Add(txt2T1.Text)
            txt1T1.Text = ""
            txt2T1.Text = ""

            txt1T1.Enabled = True
            txt2T1.Enabled = False
            txt1T1.Focus()
        End If
        If lv2T1.Items.Count = 25 Then
            txt1T1.Enabled = False
            txt2T1.Enabled = False
            btnStopT1.Enabled = True
            btnPauseT1.Enabled = False
            Timer1.Stop()
            btnNxtT1.Enabled = True
        End If

The contents of listview start at index 0. If there are 25 items in it, their indices range from 0 to 24. Putting 25 there would of course be invalid for index.

so what should i do ? :)

Hi,
It is this line here:
currrentRow = lv2T1.Items.Count()
As Scudzilla said in VB you have zero based indexes but count will give you the count of items i.e. In your list item, if you have 25 items the count will return 25 but the index of the last item will actually be 24 (25-1 = 24) because the index is zero based. So you should change the line to:
currrentRow = lv2T1.Items.Count()-1

Well, for starters, could you explain the relationship of lv1T1 and lv2T1? Based on the code you've posted, the only way I can see that gives that error is that at line 4, currrentrow would equal 25, while lv1T1 has 24 or less items.

@g Wadell : thank you for your suggestion I will try to do it, :)
@Scudzilla: "compare" is a function that compares the string from lv1T1 to the string in lv2T1. actually, what the my program supposed to do is to type the information correctly from lv1T1 in 2 textboxes and then submit it to lv2t1. when the lv2t1.items that count reached 25 (since i only have 25 items in lv1T1) all the controls will be disabled.. unfortunately, when i'm on the 25th row that error occurs.

how can i upload a print screen of my program, so that you could be able to see it

@G Wadell : I did what you told me but the error says

InvalidArgument=Value of '-1' is not valid for 'index'.

The problem lies with you assigning the currentRow to items.count. You need to find the currentRow based on some criteria - this is only pseudo:
dim intCount ,i as Integer
intCount=.....Row.count
for i = 0 to intCount-1
if ...text= ..Row(i).items.text then
currentRow=...Row(i)
next
So basically you need to find the current row and then do your stuff.

Hi
You could put a in the function to ensure there are items in the listbox:

if lv2T1.Items.Count > 0 then
    currrentRow = lv2T1.Items.Count()-1
else
    exit sub        
end if

But as Minimalist states setting using this code will ALWAYS return you the last item in lv2T1. You may want to consider using the SelectedItem property to get the currently selected listitem.

hi guys, first and foremost, thank you for your effort in sharing your knowledge on helping me with my problem. I tried and studied all your suggestions and I'm happy to inform you that I ALREADY solved it :)

thanks to you (and to my senior programmer ^_^)

so this is how i did it:

If compare(lv1T1.Items(currrentRow).SubItems(2).Text, txt2T1.Text) = True Then

i put -1 in currentRow

If compare(lv1T1.Items(currrentRow -1 ).SubItems(2).Text, txt2T1.Text) = True Then

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.