Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / February 2005

Tip: Looking for answers? Try searching our database.

SelectedIndexChanged event with listview

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jeff Brown - 16 Feb 2005 20:59 GMT
I have a form with a listview to allow users to select an item.  When they
select one I fill in controls below it for them to modify the values.  They
can also just enter new values in the controls without selecting a listview
item in order to insert new records.  After entering new data if they select
a row in the listview I prompt them to make sure they want to save the
changes.  If they select 'Cancel' I want to not highlight the newly selected
row and leave the data in the detail controls as it was.  Otherwise the new
row is selected and the listview item is used to populate the detail
controls.  

The problem seems to be that when I clear the selected items for the
listview the selectedindexchanged event fires again.  I get the yesnocancel
prompt twice.  If I step through the code in the debugger I only get it once.
I tried using a bool to skip the handler when clearing the selection but it
seems like the highlighting of the selected row then sometimes happens after
the clearing so it ends up in the selected anyway.  I tried playing with the
clicked event and others and the events don't seem to always happen in a
predictable order. Each thing I try seems to create new issues and it seems
this shouldn't be this difficult.  I've done similar things with the datagrid
but would like to use listview.

Basically want to be able to prompt the user sometimes when they select a
new listview item and if they "cancel" I want to be able to clear the
selection or set the selected item to a prior selected item without prompting
them a second time.

Any help would be appreciated.  Thanks in advance.

Jeff
Bob Powell [MVP] - 17 Feb 2005 11:55 GMT
ListView fires the SelectedIndexChanged event whenever the list is
manipulated.

Essentially you have to selectively remove or add the event handler.

Windows Forms Tips and Tricks has an example of this.

Signature

Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.

>I have a form with a listview to allow users to select an item.  When they
> select one I fill in controls below it for them to modify the values.
[quoted text clipped - 38 lines]
>
> Jeff
Jeff Brown - 17 Feb 2005 15:05 GMT
Thanks for the reply.  I am already trying to remove the handler.  When they
select cancel I am running this code:

this.lvInvoiceDetail.SelectedIndexChanged -= new
System.EventHandler(this.lvInvoiceDetail_SelectedIndexChanged);

lvInvoiceDetail.SelectedItems.Clear();

this.lvInvoiceDetail.SelectedIndexChanged += new
System.EventHandler(this.lvInvoiceDetail_SelectedIndexChanged);

The event handler is still being fired somehow.  I also checked to make sure
I wasn't adding multiple instances of the event handler to the event.  When I
step through the code in the debugger I only get one instance of the
messagebox and when I just run it I get 2.

Jeff

> ListView fires the SelectedIndexChanged event whenever the list is
> manipulated.
[quoted text clipped - 45 lines]
> >
> > Jeff
Bonnie Berent [C# MVP] - 20 Feb 2005 05:49 GMT
Jeff,

One other thing you could try is to create two Events in your ListView
sub-class, a BeforeSelected and an AfterSelected. You'd have to mess with the
Mouse Down/Up events and the Key Up/Down events to fire the proper events in
the proper order. It's a bit of work, but once done in the sub-class, you'll
never have to worry about it again.

~~Bonnie

> I have a form with a listview to allow users to select an item.  When they
> select one I fill in controls below it for them to modify the values.  They
[quoted text clipped - 25 lines]
>
> Jeff

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.