Solved

selectedIndexChanged Event is raising automatically...

Posted on 2004-03-31
6
468 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 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
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
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

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.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

728 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