Solved

SelectedIndexChanged is firing twice for a ListView

Posted on 2006-06-23
7
2,252 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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