Solved

selectedIndexChanged Event is raising automatically...

Posted on 2004-03-31
6
466 Views
Last Modified: 2012-05-04
Hi guys:

SelectedIndexChanged Event is being raised whenever I remove an item from the list. Is there a way I can suppress that event from raising?

Following is the statement which is the cause of the event generation.

this.lstItems.Items.Remove((ListItem)this.lstItems.SelectedItem);

I noticed that whenever the control goes to

'this.lstItems.SelectedItem'

it raises the event automatically. Please, let me know if my observation is correct.

IF yes, then how can I avoid it. thanks in advance

0
Comment
Question by:rxraza
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Assisted Solution

by:EBatista
EBatista earned 30 total points
ID: 10728780
hi rxraza,
what you can do is to check the sender and the eventargs parameters of the event handler and stop it if it was rized by the removing or if you find nothing you can manually control this by adding a boolean variable to your form, ie blnRemoving and set it to true just before that line that rize the event and then read it in your event handler and stop it if the variableis true, and just after the removing line set it to false again.

blnRemoving=true;
this.lstItems.Items.Remove((ListItem)this.lstItems.SelectedItem);
blnRemoving=false;

//the first line in the SelectedIndexChanged event handler:
if (blnRemovingItem){
 return;
}
 
of course it is a trick, i preffer the first approach if possible, dont have VS in this pc son i cant help you right now

good luck  
0
 

Author Comment

by:rxraza
ID: 10728963
Thanks for the response EBatista. Your second approach will work for sure. Now, what I like to know is:

Why is it raising that event automatically, if I had something like

this.lstItems.SelectedItem  = listItem;

then it would have made sense to raise the event coz the selectedItem might have changed.

but for the case of simply removing an item by the statement

this.lstItems.Items.Remove((ListItem)this.lstItems.SelectedItem);

it does not make any sense to me. DOES IT MAKE ANY SENSE TO YOU? IS THAT A BUG OR A NORMAL SCENARIO?

My second question is if I use the first approach how would I know that the event was raised by the remove method or precisely by the execution of lstItems.SelectedIndex expression because in the event handler I would get the sender which will always be the listBox and eventArgs which I do not know contains sufficient information.

Thanks in advance.
0
 
LVL 6

Expert Comment

by:KarunSK
ID: 10729135
When you remove the selected item from a list, the SelectedItem changes to Nothing and SelectedIndex to -1. This change triggers the event.

The EventArgs sent by this event is not usable, since it is only the baseclass. Any event that needs to send more information through this will derive from EventArgs and use that class (for example, the MouseMove event will pass a MouseEventArgs object).

So you have to do what EBatista said; namely, set a flag that tells you whether the change was through the UI or code.

Karun.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 6

Accepted Solution

by:
KarunSK earned 70 total points
ID: 10729250
On second thoughts though, if the item is removed then SelectedIndex = -1. So you can probably test for this scenario and proceed accordingly. Like:

    Private Sub lstItems_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstItems.SelectedIndexChanged
        If lstItems.SelectedIndex = -1 Then
            'ListItem removed via code
        Else
            'Current selection changed by user
        End If
    End Sub
0
 
LVL 10

Expert Comment

by:EBatista
ID: 10731220
well, i have reproduce the problem, strange...the event is not raised, the differences with your code and mine is that i dont do the cast to remove the item:
this.lstItems.Items.Remove(lstItems.SelectedItem);
is it a web or win app?


0
 

Author Comment

by:rxraza
ID: 10732724
EBatista:

That is a windows application. I isolate the problem and add strings to the listBox and I found that events were raised.

this.listBox1.Items.Add("item A");
this.listBox1.Items.Add("item B");
this.listBox1.Items.Add("item C");

this.listBox1.SelectedItem = "item B";
this.listBox1.Items.Remove(this.listBox1.SelectedItem);
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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