473,386 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ListBox.Items.RemoveAt causing selectedItemChanged to fire

I have a form with a ListBox and two ComboBoxes. Selecting an item in the Listbox triggers code to populate the first combobox, and selecting an item from the first combo populates the second combo. Then the code processes the selected item from ComboBox2 and removes the selected item. Each item in ComboBox2 is processed and removed, then the selected item in ComboBox1 is removed, and the user has to select another. This process is repeated until all the items in ComboBox1 are removed, then the code removes the selected item in ListBox.

In all cases, the removal is done with RemoveAt().

My observation: Removing an item from a ComboBox does not cause the selectedItemChanged event to fire, but it does for the ListBox. Why? Can I cause this to not happen?

Thanks for your help,
Matt Needles
Apr 19 '10 #1

✓ answered by Christian Binder

You could simply suppress the handling of SelectedItemChanged-Event.
That's the way, I handle these problems (and it works very well as long as there are no asynchronous operations).

Expand|Select|Wrap|Line Numbers
  1. bool suppressItemChangeEvent = false;
  2.  
  3. void fu() {
  4.   suppressItemChangeEvent = true;
  5.   listBox1.RemoveAt(0);
  6.   suppressItemChangeEvent = false;
  7. }
  8.  
  9. void listBox1_SelectedItemChanged(...) {
  10.   if(suppressItemChangeEvent)
  11.     return;
  12.  
  13.   //... your code for handling the chg-event
  14. }
  15.  
  16.  

8 3509
tlhintoq
3,525 Expert 2GB
Does it *always* fire the SelectedChanged event?
Or only if you remove the selected item, forcing a new item to become selected?
Apr 19 '10 #2
As specified, it fires when I remove the selected item. However, this does not happen when I remove the selected item in a ComboBox. Why?
Apr 21 '10 #3
tlhintoq
3,525 Expert 2GB
If you delete the selected item of a combbox, then a new item has to become selected. Its the nature of a combobox. Even if it is item index -1 meaning no item selected.

If you delete the selection of a listbox you can be left with an empty text area, yet no selection has been made/changed. Thus no event.
Apr 21 '10 #4
@tlhintoq
This doesn't answer my question: Why does removal of an item from a ComboBox NOT cause a selectedItemChanged, when it DOES for a ListBox? I need to remove an item from a ListBox and NOT get the event. How?
Apr 22 '10 #5
@tlhintoq
I think you got these backwards. ListBox has no textarea, it's comboboxes that do.
Apr 22 '10 #6
Christian Binder
218 Expert 100+
You could simply suppress the handling of SelectedItemChanged-Event.
That's the way, I handle these problems (and it works very well as long as there are no asynchronous operations).

Expand|Select|Wrap|Line Numbers
  1. bool suppressItemChangeEvent = false;
  2.  
  3. void fu() {
  4.   suppressItemChangeEvent = true;
  5.   listBox1.RemoveAt(0);
  6.   suppressItemChangeEvent = false;
  7. }
  8.  
  9. void listBox1_SelectedItemChanged(...) {
  10.   if(suppressItemChangeEvent)
  11.     return;
  12.  
  13.   //... your code for handling the chg-event
  14. }
  15.  
  16.  
Apr 22 '10 #7
tlhintoq
3,525 Expert 2GB
This doesn't answer my question: Why does removal of an item from a ComboBox NOT cause a selectedItemChanged, when it DOES for a ListBox? I need to remove an item from a ListBox and NOT get the event. How?
I know this is going to sound like your parent saying "because", but it's the truth...

Why is a NumericUpDown.Value a decimal type while a TrackBar.Value is an int type? Because that's the way Microsoft built their framework. It's the kind of inconsisency that happens when 10,000 people work on a single project. One guy thinks something should work one way so that's what he does in the 10 controls he builds. Someone else interprets the instructions differently so his 10 controls work a little different. Or there is some reason why it was done that way that makes sense under a certain condition, but that condition doesn't apply to your situation so it doesn't make sense.

When I first started learning C++ I couldn't make any progress. I kept getting upset about all these inconsistencies. I would bitch and moan and yell at the air: "WTF are these guys thinking?" ... "Why on Earth would you do xxx instead of zzz?" ... "This makes no sense!"

Then one day a new mind-set hit me. Quit fighting it. It doesn't matter *why* because I can't change it. Knowing the *why* won't change the behavior. I just needed to be able to recognize and quantify the behavior and code around it. Once I came to grips with the fact that knowing the *why* did nothing to benefit me I was a lot happier and my blood pressure a lot lower.
Apr 22 '10 #8
@ChBinder
Thank you, ChBinder. Your solution will serve me well. I agree with the philosophical response of tlhintoq, but I didn't just ask Why, I asked if there was a way to avoid that happening. Your response helps resolve my problem.
Apr 23 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dan Bass | last post by:
I'm looking to develop a listbox with in-place editing where as each item is selected, it grows to fit in all the text boxes. When the item is deselected, it shrinks back to its original size. The...
4
by: Ron | last post by:
I've got a listbox that holds a list of groups. Users can select a group, hit the remove button and the group should be removed from the listbox. The only problem is that no matter which group you...
21
by: Bilal Abbasi | last post by:
I realize that you can add items to a list box as objects so you can have access to more than just one property like the itemindex in vb6. Question I have is how do I cause the listbox to show a...
5
by: Joe Fallon | last post by:
I have a list box with 7 text values in it. I have a pair of buttons to Move Up or Move Down the selected item one position. What is the simplest way to code the buttons so the item moves one...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
0
by: John Dann | last post by:
I had a listbox that allows reordering of listed items using the mouse to drag and drop. In the DragDrop event handler, the key instructions are: MyListBox..Items.RemoveAt(PreMoveIndex)...
6
by: segue | last post by:
Hi; I need to change the order of a listbox array from a form app where I select order up or down. What's a good way to do that? Regards; Segue
5
by: Phill W. | last post by:
(VB'2003) What's the correct way to remove multiple, selected items from a ListView control (say, from a ContextMenu)? I ask because I'm getting a very annoying ArgumentOutOfRangeException...
1
by: Dom | last post by:
I selectively "disable" certain items in a ListBox. They appear gray and italicized. I want to make sure the user never selects these. Is there a way I can do this? For example, can I capture...
5
by: Academia | last post by:
(If you've seen this in the drawing NG, sorry. I inadvertently sent it there.) I have a listbox populated with Objects. The Class has a String field that ToString returns. I assume that...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.