Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Argument Out of range Except Error

Posted on 2011-09-29
14
Medium Priority
?
374 Views
Last Modified: 2012-08-14
I get this argument out of range error whenever I click on the check box on my listview. If I click the listview link (in my case its a part file path and name) I do not get this error.
VB-Error.png
0
Comment
Question by:cmdolcet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
  • +1
14 Comments
 
LVL 37

Expert Comment

by:Neil Russell
ID: 36817370
You need to surround all that with

If lvPartFiles.CheckedItems.Count <> 0 Then


End if
0
 

Author Comment

by:cmdolcet
ID: 36817413
I did what you suggested and I still get an error. Te confusing part is that this happens only when I check the checkbox. When I double click on the part file path and name link it will place a check mark in the checkbox and the error doesn;t happen. Why the difference?
For cnt = 0 To lvPartfiles.CheckedItems.Count - 1 ' lvPartfiles.SelectedItems.Count - 1 
                If lvPartfiles.CheckedItems.Count <> 0 Then
                    _File_Selected = lvPartfiles.SelectedItems(cnt).Text
                    If lvPartfiles.SelectedItems.Item(cnt).ForeColor = Color.Red Then
                        MessageBox.Show("File has no XYZ Definitions associated with it. User must Edit and Upload the XYZ Definitions.", "XYZ Definition Warning Message", MessageBoxButtons.OK, MessageBoxIcon.Warning)
                    Else
                        For Each file As String In System.IO.Directory.GetFiles(_dir_name_XML_Files, "*.xml")
                            XML_File_Name_Only = file
                            xmlFile = file
                            slashPosition = file.LastIndexOf("\")
                            XML_File_Name_Only = xmlFile.Substring(slashPosition + 1)
                            XML_File_Name_Only = System.IO.Path.GetFileNameWithoutExtension(XML_File_Name_Only)

                            'Compares the two files
                            If LMI_File Like "*########" Then
                                LMI_File = LMI_File.Remove(LMI_File.Length - 12)
                            End If
                            If LMI_File = XML_File_Name_Only Then
                                'Opens the selected XML File that matches the .LMI File selected in the List.
                                Select_XMLFile()
                                Create_Trasfer_File()

                            End If

                        Next
                    End If
                End If
                    Exit Sub
            Next

Open in new window

0
 
LVL 37

Expert Comment

by:Neil Russell
ID: 36817462
Ah listview.selecteditems index is based 1 not 0!

lvpartfiles.selecteditems.item(cnt+1)

or for cnt = 1 to .......
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 37

Expert Comment

by:Neil Russell
ID: 36817503
If lvPartfiles.CheckedItems.Count <> 0 Then  
    For cnt = 1 To lvPartfiles.CheckedItems.Count

0
 

Author Comment

by:cmdolcet
ID: 36817532
I get the same Argument out of range, just the index now has changed to 1.
VB-Error.png
0
 
LVL 4

Expert Comment

by:Ambusy
ID: 36817544
The error lies in the fact that you can SELECT items in a list view (click on a text, Shift or Ctrl-click for multiple selects)
And you can CHECK items (a V in the box)
The 2 collections CheckedItems and SelectedItems are not the same and refer to 2 completely different things.

Try: Click a text, Ctrl-click another. Then CHECK both lines. Your program will work!
0
 
LVL 4

Expert Comment

by:Ambusy
ID: 36817578
a bit of code to help you ahead:
       For cnt As Integer = 0 To ListView1.CheckedItems.Count() - 1
            Dim i As Integer = ListView1.CheckedIndices(cnt)
            Dim myText As String = ListView1.Items(i).Text ' text from checked line
        Next

Open in new window

0
 

Author Comment

by:cmdolcet
ID: 36817722
Ambusy, I see your point. However the program must work with users clicking in the checkbox or clicking the link.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36891254
>However the program must work with users clicking in the checkbox or clicking the link.
In that case, I would deal with the CheckedItems and SelectedItems separately. So loop through checkeditems and also loop through selecteditems.
0
 

Author Comment

by:cmdolcet
ID: 36891818
CodeCruiser: How can I tell if someone has done the checkitem or selecteditem? I understand your logic of seperation the two but I would need some CAse Statement of known selection.

Thanks.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 36892013
When you run the loop

i = 0 to selecteditems.count - 1

if there is no selected item, the loop will not run.
0
 

Author Comment

by:cmdolcet
ID: 36892347
OK when I run the loop I will still get an error. How can I capture this and make it go to another loop.

Try Catch?
0
 

Author Comment

by:cmdolcet
ID: 36892997
I guess I need to know how to look for the difference between a CheckedItem and Selected Item
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1000 total points
ID: 36893509
>OK when I run the loop I will still get an error.
You wont. You would only get errors if you try to access a selected item using a checked item index. Dont mix them and you should be fine.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

610 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