Solved

SelectedIndexChanged is firing twice for a ListView

Posted on 2006-06-23
7
2,206 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

751 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