Ron Carlson

How can I convert a user generated listbox to an array I have tried copy to but its not working out too well. Someone mentioned using direct cast but I have no idea where to start with that.

Re: Visual Basic Express Edition convert listbox to array

DMan1

Dim MyArray(Me.ListBox1.Items.Count) As String

Me.ListBox1.Items.CopyTo(MyArray, 0)






Re: Visual Basic Express Edition convert listbox to array

Ron Carlson

that works so far, got rid of the squiggly blue lines its not ready to run yet. But i'm confused a bit on the Me.listbox1.items.copyto(myArray,0) - what is the comma & Zero for or mean



Re: Visual Basic Express Edition convert listbox to array

Ron Carlson

ok I try to run it now but it throws an InnerException because the listbox hasn't been populated yet. How do I tell it to basically standby until the user populates the listbox.





Re: Visual Basic Express Edition convert listbox to array

Ron Carlson

HMM i populated the list with some items and ran it, same problem. The full error text is:

{"An error occurred creating the form. See Exception.InnerException for details. The error is: Object reference not set to an instance of an object."}





Re: Visual Basic Express Edition convert listbox to array

DMan1

I ran a small test with prepopulated items and then displayed the array items and it worked fine.

Which object is it pointing to as not set, the array or the listbox

It sounds like you may want to use a List (Of T) and populate the list as the user is populating the listbox

Private MyList as New List(Of String)

Private Sub AddItem(Byval Item as string)

Me.Listbox1.Items.Add(item)

MyList.Add(item)

end Sub






Re: Visual Basic Express Edition convert listbox to array

DMan1

Ron Carlson wrote:
that works so far, got rid of the squiggly blue lines its not ready to run yet. But i'm confused a bit on the Me.listbox1.items.copyto(myArray,0) - what is the comma & Zero for or mean

the comma denotes another parameter...the zero is the second parameter which tells the copy to method which element to start copying to






Re: Visual Basic Express Edition convert listbox to array

Ron Carlson

its resolved now with your first suggestion. Last night I kept getting that error, this morning when I rebooted by computer and started playing with the code again, it didn't give me any errors and I didn't change anything else. LOL so thanx anyways.