Current filter:
                                You should refresh the page.
                                Support Center
                                0
                                  • 

                                    I have a ListBoxEdit control that is used for users to select a number of items. As they are selected, they are displayed in a different
                                    control. When the user double clicks the items in the other control, they are removed and the corresponding item is unselected from the ListBoxEdit control. This all works great, unless the Select All option is used in the ListBoxEdit. In this case, all the items are displayed correctly, but if a single item is double clicked in the other control an error is thrown ("Collection was modified; enumeration may not execute").


                                    It appears to be because the SelectedIndexChanged event of the ListBoxEdit control is only firing on the removal of an item from the SelectedItems property (lstItems.SelectedItems.Remove or lstItems.SelectedItems.RemoveAt) ONLY if the SelectAll option was used to select everything. When an individual item is selected and then the removed, the event isn't triggered.

                                    Is there any way around this?

                                Show all comments
                                • Adrian Bustamante 11.01.2012

                                  Ok, so the SelectedIndexChanged event does trigger both times, but it seems to trigger earlier when the SelectAll option was used to select the items. In this case it is appears to fire before the Remove/RemoveAt piece can complete as opposed to firing afterwards.

                                • This exception is in fact raised when a collection is modified while it is being enumerated. For example, you will get this exception if you run a for-each loop against a collection and somehow modify the collection in this loop. This is because the default IEnumerator implementation doesn't support this scenario.
                                  Would you please provide us with a sample project illustrating the issue? We will examine the project and provide you with a solution.

                                • Adrian Bustamante 11.01.2012

                                  In our use, the ListBoxEdit control holds the same items throughout its lifetime. The only changes it experiences are whether items are selected or not.

                                  On the SelectedIndexChanged event, it takes all the currently selected items and copies them somewhere else.

                                  The issue we are experiencing, is that when the Select All option is used, the SelectedIndexChanged event fires in the middle of removing an item, instead of firing after (which is when it fires when individual items are selected). it's hard to include actual code at this moment because I have been tweaking some things to try and come up with a potential work around.

                                  Either way, the error is only happening when the Select All option is used. If it isn't touched, there is never an error. Is something happening behind the scenes when Select All is clicked that would change the order of when events are fired?

                                • Adrian Bustamante 11.01.2012

                                  in my tweaking, i've come across something similar. I've removed any code that does any explicit removing of items from the selected items list of the the listboxedit. Now, the only way to remove an item is to uncheck it from the UI. Again, this will trigger the SelectedIndexChanged event and update the secondary list. Everything is fine. We also have a cancel button, which when pressed, will undo any changes and reset everything to what it was before the last save. Again, everything works fine....until the select all option is used.

                                  Here is the scenario: user clicks the select all option and everything is updated correctly; the listboxedit control has all the items checked and the secondary list is populated correctly. When the user hits cancel, I am currently, in code, using the unselectall command and then repopulating the selected items list with the saved information. But, when the select all option is used, i am getting the same "collection was modified..." error when I attempt to unselectall. This error does not fire any other time.

                                  I can't help but think that this is related to the previous issue.

                                • From what I gather, the SelectedIndexChanged event is raised after all items are selected or deselected. Please provide us with a project clearly illustrating the issue, so that we can examine it, understand what you're doing, why the issue occurs and solve the issue.

                                • Adrian Bustamante 11.01.2012

                                  There is a lot of unnecessary code that I don't want to include, but here the main pieces.

                                  List<string> selectedItems <--holding the current selection for user
                                  List<string> savedItems <-- items to default back to if canceled

                                  listboxedit_SelctionIndexChangedEvent
                                  {
                                         selectedItems.Clear();
                                                  
                                         ObservableCollection<object> selectedExceptiontItems = lbeItems.SelectedItems;
                                         foreach (object obj in selectedExceptiontItems)
                                         {
                                               selectedItems.Add((string)obj);
                                         }
                                  }

                                  OnCancel
                                  {
                                         SetItems(savedItems);
                                  }

                                  SetItems(List<string> items)
                                  {
                                         lbeItems.UnSelectAll();
                                         foreach(string s in items)
                                         {
                                               lbeItems.SelectedItems.Add(s);
                                         }
                                  }

                                  It should be noted that the listboxedit is holding data that is associated to a selection from a grid, so when the row changes on the grid, the SetItems method is also called and the other variables are reset.

                                  GridFocusedRowChanged
                                  {
                                        List<string> newInfo = ...get new data
                                        savedItems = newInfo;
                                        
                                        SetItems(newInfo);
                                  }

                                  And, like I said before, if you select items individually, everything works fine. There is only an issue if the "Select All" check box is selected.

                                • Hi Adrian,

                                  Thank you for providing the code snippet. As far as I understand, this exception is raised in the SetItems method when you are calling the ListBoxEdit.UnSelectAll method. I have reproduced the problem on my side, and this behavior does not seem to be correct. I have passed this ticket to our developers so that they can determine the cause of the problem.

                                  Thanks

                                • Rob Hoglund @ CDW 06.25.2013

                                  Any updates on the resolution of this issue? I'm experiencing the same issue.

                                • Hello,

                                  Our developers have researched this behavior and fixed it. Therefore, you can request a Hot Fix. Please refer to the How to request a hotfix KB article for detailed information on how to get it.

                                You must  log in  or  register  to leave comments

                                To start a chat you should create a support ticket


                                If you need additional product information, write to us at info@devexpress.com or call us at +1 (818) 844-3383

                                FOLLOW US

                                DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, along with high-performance HTML JS Mobile Frameworks for developers targeting iOS, Android and Windows Phone. Whether using WPF, Silverlight, ASP.NET, WinForms, HTML5 or Windows 8, DevExpress tools help you build and deliver your best in the shortest time possible.

                                Copyright © 1998-2014 Developer Express Inc.
                                All trademarks or registered trademarks are property of their respective owners