Solved

selectedIndexChanged Event is raising automatically...

Posted on 2004-03-31
6
465 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

911 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

18 Experts available now in Live!

Get 1:1 Help Now