?
Solved

selectedIndexChanged Event is raising automatically...

Posted on 2004-03-31
6
Medium Priority
?
469 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
[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
  • 2
  • 2
  • 2
6 Comments
 
LVL 10

Assisted Solution

by:EBatista
EBatista earned 120 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 6

Accepted Solution

by:
KarunSK earned 280 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

770 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