[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

ListView SelectedIndexChanged trouble getting SelectedItems

Posted on 2006-06-03
2
Medium Priority
?
1,734 Views
Last Modified: 2007-12-19
Greetings,

I'm handleing the correct event when the user changes the selected item, but when I go and try to get that item, I'm getting null.  I use

private ListViewItem GetSelection( ListView listView )
{
    foreach ( ListViewItem listViewItem in listView.SelectedItems )
    {
        return listViewItem;
    }
    return null;
}

to get the one and on y choice in my single selection ListView.  I know there's a better way to do this, and if you have it I'd love to see it.  But the problem is that there the foreach does not get entered into, even thoiugh I only call this function when the user has selected an item in the ListView.  Can anybody suggest a solution?

Thanks,
Bob
0
Comment
Question by:ba272
2 Comments
 
LVL 11

Accepted Solution

by:
vo1d earned 2000 total points
ID: 16826627
why dont you make something like this:

internal ListViewItem _selectedItem; //<-- classmember field
       
//eventhandler for your listviews
private void ListViewSelectedIndexChanged(object sender, EventArgs e)
{
  ListView listView = sender as ListView;
  this._selectedItem = (listView.SelectedItems.Count > 0 ? listView.SelectedItems[0] : null);
}

_selectedItem is null or the selected listviewitem.
0
 

Author Comment

by:ba272
ID: 16939083
thanks. works great.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

834 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