Solved

ListView SelectedItems

Posted on 2004-08-15
2
3,450 Views
Last Modified: 2008-01-09
Hello all,

I'm trying to get item text out of selected item of a list view. Following event code works out once:

private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   textBox1.Text=listView1.Items[listView1.SelectedIndices[0]].Text;
}

When selecting another item afterwards,  I get 'System.ArgumentOutOfRangeException'. Why?

Any hints are welcome!
0
Comment
Question by:zaratin
2 Comments
 
LVL 10

Accepted Solution

by:
ptmcomp earned 125 total points
ID: 11803770
SelectedIndexChanged will also be fired when no item is selected. Therefore you must check first if SelectedIndices.Count is greater than 0.

private void listView1_SelectedIndexChanged(object sender, System.EventArgs e)
{
   if (listView1.SelectedIndices.Count > 0)
   {
      textBox1.Text=listView1.Items[listView1.SelectedIndices[0]].Text;
   }
}
0
 

Author Comment

by:zaratin
ID: 11804257
Thanks for your answer ptmcomp.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

839 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