Solved

SelectedIndexChanged is firing twice for a ListView

Posted on 2006-06-23
7
2,156 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone 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

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 124
Finally almost got it 5 48
Reading the Web.Config using IIS 7.5? 4 48
Set value of dynamically created checkboxlist 2 42
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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