Solved

SelectedIndexChanged is firing twice for a ListView

Posted on 2006-06-23
7
2,186 Views
Last Modified: 2008-01-09
Hi,

I need to be notified when the user changes a ListView selection.  Am I handling the right event?

The event gets fired twice for some reason, and I can't find the problem.  So I thought I'd be smart and use a boolean variable to call my code once, but not the second time.  The code commented out shows my failed attempt to hack a solution.

Any ideas?

Thanks,
Bob

private bool intializingDeliveryRun = true;

private void driverRunsLV_SelectedIndexChanged(object sender, System.EventArgs e)
{
//if ( intializingDeliveryRun )
//{
      addressListBox.SelectedIndex = -1;
      SelectDriverRun();            
      intializingDeliveryRun = false;
/*}
else
{
      intializingDeliveryRun = true;
}*/
}
0
Comment
Question by:ba272
  • 3
  • 3
7 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 16974157
To understand what happens it is necessary to have more information.
When is it called twice? When user changes selection? When program sets SelectedIndex?
Notice that all .NET controls raise change events both as result of user actions, and when control state is changed programmatically.
0
 

Author Comment

by:ba272
ID: 16974169
Alex,

I was careful to check to see I was not changing it programmatically.  I'm just talking about the user changing ther selection.

I just ran a test and commented out this line:

     SelectDriverRun();          

and it still calls the function twice.

Thanks for the help,
Bob
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 250 total points
ID: 16974187
Maybe you subscribed twice to this event or call to this function from some other place? Make search in the project for "driverRunsLV_SelectedIndexChanged" string.

addressListBox.SelectedIndex = -1;

What happens in the addressListBox SelectedIndexChanged handler?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ba272
ID: 16974208
I'm testing it now.  I actually had coded it with a similar hack, and I'm running it to find why that one works.

0
 
LVL 48

Expert Comment

by:AlexFM
ID: 16974219
You can also stop in the debugger and test calling stack.
0
 

Author Comment

by:ba272
ID: 16974234
Thanks.  I'll keep working it.

Bob
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 250 total points
ID: 16987176
Bob,

This is usually how I block multiple calls:

private void driverRunsLV_SelectedIndexChanged(object sender, System.EventArgs e)
{

if ( !intializingDeliveryRun )
{
     intializingDeliveryRun = true;
     addressListBox.SelectedIndex = -1;
     SelectDriverRun();          
     intializingDeliveryRun = false;
}

Then, the call for addressListBox.SelectedIndex = -1; won't fire the event twice.

Bob
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

679 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