wessamzeidan Posted February 9, 2004 Posted February 9, 2004 How can I get the selected item in a listview. I'm using the SelectedIndexChanged event of the listview to get the current selected index but the first time I click I always get the first item no matter which item I click, and the second time I click I get an error telling me that nothing is selected... Any one has any idea??? Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
I Schobert Posted February 9, 2004 Posted February 9, 2004 Wess, The reason why you have nothing selected at some point is because this event is fired twice upon selection of another listview item. I would suggest that you test for selection and take action only if an item is selected. The first time around, there will be no selection, the second time should have the new item selected. Use code like this: If lstListView.SelectedIndices.Count > 0 Then 'do what you need to do 'like this: MessageBox.Show(lstListView.SelectedItems(0).Text) 'this will give you the text of the first selected item (in case 'multiple are selected End If I hope this helps... Quote IS
wessamzeidan Posted February 10, 2004 Author Posted February 10, 2004 Thank alot, it worked. But why does the event get raised twice, is it a bug!!!! Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
I Schobert Posted February 10, 2004 Posted February 10, 2004 No, that is not a bug. First, it is raised when the currently selected ListView Item is "un-selected". At that point, no Item is selected. Next, it is raised when the new Item is selected, and that is when you want to get your code to fire... I am glad it helped. Quote IS
wessamzeidan Posted February 10, 2004 Author Posted February 10, 2004 thanks alot Quote Proudly a Palestinian Microsoft ASP.NET MVP My Blog: wessamzeidan.net
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.