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

Re: SelectedIndexChanged event with listview by Bob

Bob
Thu Feb 17 05:55:26 CST 2005

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.

--
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.





"Jeff Brown" <Jeff Brown@discussions.microsoft.com> wrote in message
news:02C742E1-9170-4561-A35F-FC4BCB4D60F8@microsoft.com...
>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
>



Re: SelectedIndexChanged event with listview by JeffBrown

JeffBrown
Thu Feb 17 09:05:02 CST 2005

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

"Bob Powell [MVP]" wrote:

> 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.
>
> --
> 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.
>
>
>
>
>
> "Jeff Brown" <Jeff Brown@discussions.microsoft.com> wrote in message
> news:02C742E1-9170-4561-A35F-FC4BCB4D60F8@microsoft.com...
> >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
> >
>
>
>

RE: SelectedIndexChanged event with listview by BonnieBerentCMVP

BonnieBerentCMVP
Sat Feb 19 23:49:02 CST 2005

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

"Jeff Brown" wrote:

> 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
>