Solved

selectedIndexChanged Event is raising automatically...

Posted on 2004-03-31
6
464 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

759 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

20 Experts available now in Live!

Get 1:1 Help Now