Solved

listview box - InvalidArgument=Value of '0' is not valid for 'index'.

Posted on 2008-06-24
30
11,835 Views
Last Modified: 2010-04-21
hoping to find wayne here been very helpful with another question but really if anyone else knows please help me out getting very pissed wit hte code and have been looking all afternoon for an answer


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

The weird this is if i call this form up blank and call the modal with a button on the form all works fine

Any ideas guys

System.ArgumentOutOfRangeException was unhandled
  Message="InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index"
  ParamName="index"
  Source="System.Windows.Forms"



****code for module*******

    Public Sub stoo()

        Dim rs As New ADODB.Recordset

        Dim sql As String

        Dim objlst As ListViewItem

        Dim objlst1 As ListViewItem

        Form1.spel.Items.Clear()

        Form1.stgl.Items.Clear()

        Form1.ListView2.Items.Clear()

        Form1.studentnotes.Text = ""

 

 

        sql = "select * from [Student_Outcomes] where [StudentID] = " & Form1.TextBox7.Text

 

        Debug.Print(sql)

        rs.Open(sql, cnn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)

        Do Until rs.EOF

            With Form1.ListView1

 

                objlst = Form1.ListView2.Items.Add(rs.Fields("OutcomeDesc").Value)

                objlst.SubItems.Add(rs.Fields("outcomedate").Value)

                objlst.SubItems.Add(rs.Fields("OutcomeAuth").Value)

                objlst.SubItems.Add(rs.Fields("OutcomeProgram").Value)

                objlst.SubItems.Add(rs.Fields("id").Value)

            End With

 

 

            rs.Update()

            rs.MoveNext()

 

        Loop

        rs.Close()

 

        sql = "select * from [Student_Programs] where [StudentID] = " & Form1.TextBox7.Text

        Debug.Print(sql)

        rs.Open(sql, cnn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)

        Do Until rs.EOF

            With Form1.spel

 

                objlst1 = Form1.spel.Items.Add(rs.Fields("StudentProg").Value)

                objlst1.SubItems.Add(rs.Fields("StudentProgRego").Value)

            End With

 

 

            rs.Update()

            rs.MoveNext()

 

        Loop

        rs.Close()

 

        sql = "select * from [Student_TargetGroups] where [StudentID] = " & Form1.TextBox7.Text

 

        Debug.Print(sql)

        rs.Open(sql, cnn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockPessimistic)

        Do Until rs.EOF

            With Form1.stgl

 

                objlst1 = Form1.stgl.Items.Add(rs.Fields("StudentTG").Value)

                'objlst1.SubItems.Add(rs.Fields("StudentProgRego").Value)

            End With

 

 

            rs.Update()

            rs.MoveNext()

 

        Loop

        rs.Close()

    End Sub

End Module

 

 

***** code for listview *****

 

 

 Dim rs As New ADODB.Recordset

        Dim datesel As Date

        Dim sql As String

        Dim idd As String

        Dim test As String

        studentnotes.Text = ""

        idd = ListView2.SelectedItems.Item(0).SubItems(4).Text

        ' Debug.Print(idd)

        ' test = ListView2.SelectedItems.Item(0).SubItems(1).Text

        ' Debug.Print(test)

 

        datesel = Date.ParseExact(ListView2.SelectedItems.Item(0).SubItems(1).Text, "dd/MM/yyyy", My.Computer.Info.InstalledUICulture)

        Debug.Print(datesel)

        sql = "select * from [Student_Outcomes] where [OutcomeDate]  = #" & datesel.ToString("dd-MM-yyyy") & "# And [StudentID] = " & TextBox7.Text & "  And [id] = " & idd

        Debug.Print(sql)

        rs.Open(sql, cnn1, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockReadOnly)

        studentnotes.Text = ""

        If Not (rs.EOF And rs.BOF) Then

            If Not IsDBNull(rs.Fields("OutcomeNotes").Value) Then

                studentnotes.Text = rs.Fields("OutcomeNotes").Value

                Debug.Print(rs.Fields("outcomenotes").Value)

            End If

        End If

        rs.Close()

Open in new window

0
Comment
Question by:awolarczuk
  • 15
  • 14
30 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
Which line causes the error?
0
 

Author Comment

by:awolarczuk
Comment Utility
idd = ListView2.SelectedItems.Item(0).SubItems(4).Text

then if i comment that out the next one in the chain
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
OK, are you sure there's a selected item in the ListView? Also, if you run the code from the ListView.SelectedIndexChanged() event, the event is fired twice, once when the original item is de-selected, then again when the new item is selected. When the first item is de-selected, the SelectedItems.Count will be 0. To handle this you need to check that there is at least one item selected, using something like this....

    If ListView2.SelectedItems.Count > 0 Then
        'your code
    End If

Wayne
0
 
LVL 6

Expert Comment

by:psychic_zero
Comment Utility
I think you code want to set ListView index starting with 0. If i'm not mistaken ListView index is starting with 1. Can you tell which line this error has occured?
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
psychic_zero,

The ListView's Items collection is zero based, as are most collections/arrays in .Net.

Wayne
0
 

Author Comment

by:awolarczuk
Comment Utility
wayne if you rem the screen shot it is that data i am clicking on so there is data there basicly if i click on a button on the screen that call the same code again (same module) then it works fine, even if i just load the form blank and then call the module with the button work great it is when i am calling it when loading the form

I have even tried leaving it for 10 sec with a timer and then calling the button event and i still get the same error
0
 

Author Comment

by:awolarczuk
Comment Utility
it is set in the hover event of the list view box
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
awolarczuk,

I notice in your other question ( http:/Q_23513234.html - which will have to be deleted BTW, because it's a duplicate of this one), that you are calling your code in the ListView.MouseHover event. If so, you should handle it like the snippet below.

Wayne
    Private Sub ListView2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView2.MouseHover

        Dim ht As ListViewHitTestInfo = ListView2.HitTest(Windows.Forms.Cursor.Position)

        If ht.Location = ListViewHitTestLocations.Label Then

            'your code, replacing all instances of ListView2.SelectedItems.Item(0) with ht.Item

        End If

    End Sub

Open in new window

0
 

Author Comment

by:awolarczuk
Comment Utility
no problem wayne eih the other question, just a quick question why does my code work when call it with a button on the page and not when the form loads
0
 

Author Comment

by:awolarczuk
Comment Utility
sorry it is itemMouseHover
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
Are you sure there is at least one item selected before you move the mouse over the ListView? If there isn't, you will get the error.

Wayne
0
 

Author Comment

by:awolarczuk
Comment Utility
it selects it when i hover over it, like i said it works when i call it from the form that why it confusing me
0
 

Author Comment

by:awolarczuk
Comment Utility
now it doesnt bring up the error but doesnt show any data at all the skips all my code
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
It's likely that when you are hovering over the listview that the SelectedItems.Count will equal 0 when it's changing items. When you call it from the form (button.click event??), there is no change of items, therefore SelectedItems.Count will never equal zero.

Wayne
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
Perhaps try this instead....
    Private Sub ListView2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView2.MouseHover

        Dim ht As ListViewHitTestInfo = ListView2.HitTest(Windows.Forms.Cursor.Position)

        If Not ht.Item Is Nothing Then

            'your code, replacing all instances of ListView2.SelectedItems.Item(0) with ht.Item

        End If

    End Sub

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:awolarczuk
Comment Utility
ok so i will tell you the events when it works the way it is ment to

from the main form i open this form
click the button that call the modual and fill the listview box
hover the listviewbox with itemMouseHover
select text appeat in the text box

when it doesnt work (and the way i need it to work)
from the main form select the client
form 2 opens using the information provided by form1 to find the client (calling the Modual)
hover over the item in the listview box

get error
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
...or forget the HitTest, and check the SelectedItems.Count....
    Private Sub ListView2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView2.MouseHover

        If ListView2.SelectedItems.Count > 0 Then

            'your code, still using ListView2.SelectedItems.Item(0)

        End If

    End Sub

Open in new window

0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
Can't really tell without seeing it for myself. But one of my suggestions will fix the problem.

Wayne
0
 

Author Comment

by:awolarczuk
Comment Utility
ok mate that seem to work but i have to click on any of the items first any way around that, when i hover over the item it  selects it
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
ya, you have the HoverSelection property of the listview set to True. I see what's going on now though. If you move the mouse away from the listview, then bring it back on another item, your code will run, but it won't when simply moving within the ListView. You'll need to use the MouseMove event instead, and do something like this....
    Private SelectedItem As ListViewItem
 

    Private Sub ListView2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView2.MouseMove

        If ListView2.SelectedItems.Count > 0 Then

            Dim ht As ListViewHitTestInfo = ListView2.HitTest(e.Location)

            If Not SelectedItem Is ht.Item Then

                'your code, replacing all instances of ListView2.SelectedItems.Item(0) with ht.Item

                SelectedItem = ht.Item

            End If

        End If

    End Sub

Open in new window

0
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 500 total points
Comment Utility
Or even better would be to simply use the SelectedIndexChanged event....
    Private Sub ListView2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView2.SelectedIndexChanged

        If ListView2.SelectedItems.Count > 0 Then

            'your code, still using ListView2.SelectedItems.Item(0) 

        End If

    End Sub

Open in new window

0
 

Author Comment

by:awolarczuk
Comment Utility
with the over over item it will run again once i have clicked in the listview, i have the hoverselect on and had no difference still have to click in the listview box
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
As I mentioned in my last post, you'd be better of with the SelectedIndexChanged event. This will work because the HoverSelection property is set to true, which does all the selecting for you, thereby negating any need to handle a mouve event.

Wayne
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
If all you want to do is show some text in a Textbox of the Item you are hovering over, turn HoverSelection off, then use the MouseMove event like below. This way there's no need to select an item.

Wayne
    Private Sub ListView2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView2.MouseMove

        Dim ht As ListViewHitTestInfo = ListView2.HitTest(e.Location)

        If Not SelectedItem Is ht.Item Then

            'your code, replacing all instances of ListView2.SelectedItems.Item(0) with ht.Item

            SelectedItem = ht.Item

        End If

    End Sub

Open in new window

0
 

Author Comment

by:awolarczuk
Comment Utility
ok thanks wayne i have gone to the selectindex change but still have an issue when i fist go in the the form i have to click on the list box to get focus and for it to work then after that it is fine
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
Sorry, the code snippet should have been this....
    Private SelectedItem As ListViewItem
 

    Private Sub ListView2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView2.MouseMove

        Dim ht As ListViewHitTestInfo = ListView2.HitTest(e.Location)

        If ht.Location = ListViewHitTestLocations.Label Then

            If Not SelectedItem Is ht.Item Then

                'your code, replacing all instances of ListView2.SelectedItems.Item(0) with ht.Item

                SelectedItem = ht.Item

            End If

        End If

    End Sub

Open in new window

0
 

Author Comment

by:awolarczuk
Comment Utility
what it has to do it fid data in the listview box and then using that do a query and find the data in the database and return the info to the text box
0
 

Author Comment

by:awolarczuk
Comment Utility
so what i have in the index select will work great thanks to you, but i would love if i didnt have to click once on the listview box to get the hover part of it working
0
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
Comment Utility
Use the last of my suggestions (MouseMove event with HoverSelection = False).

Below is a simple example of using the MouseMove event. Create a new windows project, add a Textbox and a ListView (don't worry about setting any properties in design view), then replace the entire code with the below snippet. It shows the MouseMove event working wonderfully, without having to clunkily select an item.

As for having to click the listview before the mouse hover works, I have no idea why it's not working because I haven't seen any code where you show the form. I find it hard to believe you *need* to select an item in the listview, because if you *needed* to select an item, you would not have got the error in the first place, because your code would not have run. Anyway, you can 'select' the listview by using this line in the Form.Load event....

    ListView2.Focus()

...and you can continue to use the SelectedIndexChanged event.

Wayne
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        With ListView1

            For i As Integer = 1 To 3

                .Columns.Add("Col" & i)

            Next

            .View = View.Details

            .HoverSelection = False

            .MultiSelect = False

            .FullRowSelect = True

            .Width = 200

        End With

        For r As Integer = 1 To 5

            With ListView1.Items.Add("Item" & r)

                .SubItems.Add(r & " - Sub1")

                .SubItems.Add(r & " - Sub2")

            End With

        Next

    End Sub
 

    Private Sub ListView1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseMove

        Dim ht As ListViewHitTestInfo = ListView1.HitTest(e.Location)

        Static SelectedItem As ListViewItem

        If ht.Location = ListViewHitTestLocations.Label Then

            If Not SelectedItem Is ht.Item Then

                TextBox1.Text = ht.Item.Text

                SelectedItem = ht.Item

            End If

        End If

    End Sub

End Class

Open in new window

0
 

Author Closing Comment

by:awolarczuk
Comment Utility
Thanks wayne as always good to work with
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now