Solved

SelectedIndexChanged is firing twice for a ListView

Posted on 2006-06-23
7
2,077 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

705 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now