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

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

34 Replies - 2922 Views - Last Post: 23 November 2015 - 09:55 AM Rate Topic: -----

#1 Kelvin Nyota   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

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

Posted 19 November 2015 - 04:22 PM

I am getting this error here: InvalidArgument=Value of '1' is not valid for 'index'.
Parameter name: index

What is being autofilled is from these sections of the code:

Section1
If wordParts.Count >= 2 Then
AutoComplete.Add(wordParts(0).ToString & wordParts(1).ToString)
End If


Section2
For Each auto As String In AutoComplete

If auto.StartsWith(wordParts) = True And wordParts <> "" And wordParts <> " " Then


The actual error is coming from here, but I wanted you to trace the path of the problem:


Dim textToAppend As String
Dim x As Integer
'This has a parameter because it's possible to select multiple items - 0 is the first item of the multiple items if there are any

For x = 0 To ListView1.SelectedItems.Count - 1


textToAppend = ListView1.SelectedItems.Item(x).SubItems(1).Text
RichTextBox1.SelectedText = textToAppend
Next

Let me know who can help me with this in VB


ListView1.Items.Add(New ListViewItem(auto))

Is This A Good Question/Topic? 0
  • +

Replies To: InvalidArgument=Value of '1' is not valid for 'index'.

#2 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1510
  • View blog
  • Posts: 3,817
  • Joined: 01-February 13

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

Posted 19 November 2015 - 05:01 PM

My guess is that you do not have any items in the Listview OR you do not have a SubItem added to the ListViewItem.

Do you have the View property of the ListView set to Details?

Have you added 2 Columns to the ListView?

If not then that is why you have an error when trying to access SubItem(1).

If you have set those properties then you will need to show us the code you use to add the Items and SubItems to the ListViewItem. Please use a CODE block when poosting your code.

This post has been edited by IronRazer: 19 November 2015 - 05:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 Kelvin Nyota   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

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

Posted 19 November 2015 - 05:06 PM

Yes, I have items in my ListView. The only problem comes when I click on an item to for it to appear on the RichTextBox. Yes, I have set my ListView property to details

It has two columns with Column headers.

This post has been edited by andrewsw: 19 November 2015 - 05:08 PM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw   User is offline

  • Unprocessable Entity
  • member icon

Reputation: 6594
  • View blog
  • Posts: 26,831
  • Joined: 12-December 12

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

Posted 19 November 2015 - 05:09 PM

ListView1.Items.Add(New ListViewItem(auto))

I just want to mention that auto is an unreserved keyword:

MSDN said:

.. which means you can use them as names for your programming elements. However, doing this is not recommended, because it can make your code hard to read and can lead to subtle errors that can be difficult to find.


Note that you don't have to quote the previous post in full, there is a Reply button further down the page, or use the Fast Reply box.
Was This Post Helpful? 1
  • +
  • -

#5 Kelvin Nyota   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

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

Posted 19 November 2015 - 05:10 PM

I will replace the names, give me one second as I reply to you.
Was This Post Helpful? 0
  • +
  • -

#6 Kelvin Nyota   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

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

Posted 19 November 2015 - 05:18 PM

I still see no change, could this be the problem:

If ListView1.Items.Count <> 0 Then

                ListView1.Visible = True
                ListView1.GetItemAt(0, True)
            End If




Because when I try to click on an item is when the problems appear. I have just changed the names auto but nothing has changed.
Was This Post Helpful? 0
  • +
  • -

#7 Kelvin Nyota   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

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

Posted 19 November 2015 - 05:37 PM

Can anybody help me on this code please:


Dim textToAppend As String
Dim x As Integer
'This has a parameter because it's possible to select multiple items - 0 is the first item of the multiple items if there are any

For x = 0 To ListView1.SelectedItems.Count - 1


textToAppend = ListView1.SelectedItems.Item(x).SubItems(1).Text
RichTextBox1.SelectedText = textToAppend

Next



Possible places to look for this error

While Not sr.EndOfStream
                    Dim wordParts = sr.ReadLine.Split("|"c)
                    If wordParts.Count >= 2 Then
                        AutoComplete.Add(wordParts(0).ToString & wordParts(1).ToString)
                    End If
                End While


Possible place to look for the error

 For Each Apex As String In AutoComplete

                If Apex.StartsWith(wordParts) = True And wordParts <> "" And wordParts <> " " Then


                    ListView1.Items.Add(New ListViewItem(Apex))


                End If

            Next

            If ListView1.Items.Count <> 0 Then

                ListView1.Visible = True
                ListView1.GetItemAt(0, True)
            End If


Was This Post Helpful? 0
  • +
  • -

#8 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1510
  • View blog
  • Posts: 3,817
  • Joined: 01-February 13

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

Posted 19 November 2015 - 07:38 PM

You are adding a ListViewItem in the line below but, you are not adding a SubItem to that ListViewItem. So, when you try to access SubItem(1) you get an exception because, there is no SubItem added.
ListView1.Items.Add(New ListViewItem(Apex))


When you have a ListView with 2 Columns and you want to add an item, you need to add the SubItems too. For example,
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SomeString1 As String = "In Column 1"
        Dim SomeString2 As String = "In Column 2"

        Dim lvi As ListViewItem = ListView1.Items.Add(SomeString1) 'add the ListViewItem (Column1)
        lvi.SubItems.Add(SomeString2) 'add the first SubItem to the ListViewItem (Column2)

    End Sub



Or you can do it this way
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim SomeString1 As String = "In Column 1"
        Dim SomeString2 As String = "In Column 2"

        Dim lvi As New ListViewItem(SomeString1) 'add the ListViewItem (Column1)
        lvi.SubItems.Add(SomeString2) 'add the first SubItem to the ListViewItem (Column2)

        ListView1.Items.Add(lvi) 'add the new ListViewItem to the ListView
    End Sub


Was This Post Helpful? 0
  • +
  • -

#9 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1510
  • View blog
  • Posts: 3,817
  • Joined: 01-February 13

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

Posted 19 November 2015 - 07:43 PM

Actually, if you look back to your first question where i showed you an example of adding the strings to a ListView at the link below, i showed you the same thing in that code.
                'add the first value to a new ListViewItem (Column 1)
                Dim lvi As ListViewItem = ListView1.Items.Add(rowData(0))
                lvi.SubItems.Add(rowData(1)) 'add the second value to the first subitem (Column 2)



load a file with two columns into a Listbox
Was This Post Helpful? 0
  • +
  • -

#10 Kelvin Nyota   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

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

Posted 20 November 2015 - 02:03 PM

It is working now, but why is it not separating the strings properly in the columns with this: split("|"c)

Because when you look at Column one, you will see the fields that are supposed to be delimited by this: | like this: yur|your

instead of each word having being separated by each column. When you look at Column 2, the same applies, you still see the same words like this: yur|your. I don't know what's wrong with the code, but I have decide to share it:

            For Each Apex As String In AutoComplete

                If Apex.StartsWith(wordParts) = True And wordParts <> "" And wordParts <> " " Then

                    Dim lvi As New ListViewItem(Apex(0))
                    lvi.SubItems.Add(Apex(1))
                    ListView1.Items.Add(lvi)

                End If

            Next





Hay, and thank you for remembering me!
Was This Post Helpful? 0
  • +
  • -

#11 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1510
  • View blog
  • Posts: 3,817
  • Joined: 01-February 13

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

Posted 20 November 2015 - 02:40 PM

My first guess is that you are not splitting the Apex String on the "|" character. Perhaps this is what you need to do.
        For Each Apex As String In AutoComplete

            If Apex.StartsWith(wordParts) = True And wordParts <> "" And wordParts <> " " Then

                'Split the Apex string into 2 words on the | character
                Dim words() As String = Apex.Split("|"c)

                Dim lvi As New ListViewItem(words(0)) 'add the 1st word
                lvi.SubItems.Add(words(1)) 'add the 2nd word
                ListView1.Items.Add(lvi)

            End If

        Next


Was This Post Helpful? 0
  • +
  • -

#12 Kelvin Nyota   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

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

Posted 20 November 2015 - 03:10 PM

There is no error, but the code is not producing any results in the ListView. Could it be that the problem Lies elsewhere?

I'll get back to you tomorrow. I have to get some sleep. Nice time.
Was This Post Helpful? 0
  • +
  • -

#13 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1510
  • View blog
  • Posts: 3,817
  • Joined: 01-February 13

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

Posted 20 November 2015 - 04:30 PM

Well, i can`t really tell what is going on in your code without seeing the whole code and seeing the TextFile you are reading lines from. Tomorrow, you will have to post a full example of your code that reads the text file and an example of the Text file`s Text that you are reading. 8)

I have to go to work tomorrow so, i probably wont be around until about 1 or 2 PM EST.

This post has been edited by IronRazer: 20 November 2015 - 04:32 PM

Was This Post Helpful? 0
  • +
  • -

#14 Kelvin Nyota   User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 63
  • Joined: 14-November 15

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

Posted 21 November 2015 - 06:51 AM

Can you do this in VB?

Dim words2, word3 = words1.Split("|")

Dim lvi As New ListViewItem(words2) 

                        lvi.SubItems.Add(words3) 'add the 2nd word

                        ListView1.Items.Add(lvi)



Was This Post Helpful? 0
  • +
  • -

#15 IronRazer   User is offline

  • Custom Control Freak
  • member icon

Reputation: 1510
  • View blog
  • Posts: 3,817
  • Joined: 01-February 13

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

Posted 21 November 2015 - 11:31 AM

No, the String.Split Method returns an Array of Strings so, you need to assign it to an Array. That is what the () after the variable indicates, that it is an Array.

Suppose a line from your text file is like this with the pipe characters "|" separating each piece of data in the line.

John|Doe|23

Now suppose you have that line of text assigned to a String Variable called line.

Dim line as String = "John|Doe|23"

Then you split the String on the "|" characters into a String Array called words.

Dim words() As String = line.Split("|"c)

Now the words Array will contain 3 elements (Strings) like this.

words(0) would be "John"
words(1) would be "Doe"
words(2) would be "23"

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3