Solved

ListView SelectedItems

Posted on 2004-08-15
2
3,447 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now