Get items from Listbox into array

webguy55

Member
Joined
Feb 14, 2010
Messages
12
Programming Experience
Beginner
Hello,

I am trying to get the items out of a listbox and put into a text box .. I am putting the listbox items into an array and I keep getting this error:

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

bolded code where this occurs

here is my code:

VB.NET:
Private Sub btn_BuildText_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_BuildText.Click

        Dim delimiter As String         'Holds the String character representing the chosen delimiter
        Dim i As Integer                'Loop counter
        Dim tempString As String        'Holds the input string from the text box

        tempString = txt_UserInput.Text

        If rb_Comma.Checked Then
            delimiter = ","
        ElseIf rb_CRLF.Checked Then
            delimiter = "& vbCRLF"
        ElseIf rb_Space.Checked Then
            delimiter = " "
        Else
            MessageBox.Show("You must select a delimiter!")
            Exit Sub
        End If

        Dim stringArray() As String = Split(tempString, delimiter)

        For i = 0 To lb_BuildText.Items.Count
            [B]stringArray(i) = lb_BuildText.Items(i).ToString()[/B]
        Next
        txt_UserInput.Text &= stringArray(i) & delimiter


    End Sub
 
txtUserInput most likely is empty? Therefore splitting it results in an array with only 1 element?
And why do you put the LB items into an array first And why is the append command behind the loop?

Why don't you simply use for each over the listbox.items collection?
 
Guess i should of explained the process.

Basically a user types in items in a Text box...they choose the delimiter that they type in the box after each entry( IE: 1,2,3,4...) they click the button and the items they typed it get extracted between the delimiters...so then the list box get populated with those items:

1
2
3
4

So then the other button will take the items from the listbox and send them back to the Textbox.

So I was thinking since I put the items into an array first and used the split to extract the items I would do kind of the same thing here.
 
You are going to need to re-work this section
VB.NET:
 Dim stringArray() As String = Split(tempString, delimiter)

        For i = 0 To lb_BuildText.Items.Count
            stringArray(i) = lb_BuildText.Items(i).ToString()
        Next
        txt_UserInput.Text &= stringArray(i) & delimiter

You are using the size of the ListItemCollection (.Items) of the 'lb_BuildText' control to determine your loop. But inside you are also using the 'stringArray' array. So if 'stringArray' and 'lb_BuildText.Items do not have the same number of items then you will get an error, like you have got. Also I think you might need to move the last line I showed you inside the For Loop. Right now all you are doing is adding the last item in lb_BuildText.Items to txtUserInput. Finally, once you get your code working you will find that your output will have a trailing delimiter. You might need to trim that off.
 
Rather than type out a really long post where there's a chance things could get messed up in translation, here's a quick demo I whipped up.
 
Nice code...to bad im not that far into VB to understand the thinking behind it..but I kind of get the jist of it. But thank you for taking the time..i will def keep this for reference :)

actually looking through your code I noticed the use of ControlChars.CrLf and that fixed my "Enter" as a delimiter problem...THANKS!!!
 
Back
Top