Not only was the answer correct, but you went above and beyond and explained both a "why" and a "how" (as in how you got there)
awesome answer!
Main Topics
Browse All TopicsVisual Studio 2005
Visual Basic.net
Windows Application
I need to simply return a selected item text value in a listview to a textbox when an item is clicked.
I am populating the ListView with a simple file collection.
The following code works ONCE,. then when I click on another item in the listview, I get an index error.
Code:
TextBox1.Text = ListView1.SelectedItems(0)
Error is:
"InvalidArgument=Value of '0' is not valid for 'index'. Parameter name: index"
I would also like to loop through multiple selections but first need to return one...
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: FocusynPosted on 2008-07-07 at 12:03:15ID: 21947657
I didn't try this in VS.NET 2005, but I have 2003 and 2008 on my machine and tested...
nged
em(0).Text
ount - 1) em(i).Text & vbcrlf
When I put a ListView on a form and double-clicked to edit code, the default event it opened was ListView1_SelectedIndexCha
I got the same behavior you describe. I manually went in to code view, and added event handler for ListView1_Click, which sounds more appropriate anyway as you describe your need. In that event handler, I entered the following code:
TextBox1.Text = ListView1.SelectedItems.It
I rarely program forms apps so I can't tell you off the top of my head why this behavior occurs, but based on what I know about VB and .NET framework, I'd guess that the indexchanged event fires your code off at an inappropriate time. ListViews can be 'multi-dimensional' I guess when you use columns and whatnot, so indexchanged is not the same thing as click, or even close. This is why ListView has the SelectedIndices properties. At any rate, running the code on the ListView1.Click event rather than indexchanged event seems to work as I think you desire.
When you wish to loop through, you could probably use code below, still in the .Click event, with a loop on items.Count It will work if you are using a simple ListView with no complex properties etc:
Private Sub ListView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.Click
Dim i as integer
TextBox1.Text = ""
For i = 0 to (ListView1.SelectedItems.C
TextBox1.Text &= ListView1.SelectedItems.It
Next
End Sub
'The & vbcrlf adds a line break - I"m assuming multiline textbox but you can format the string as
'you wish