ListBox throws an ArgumentOutOfRangeException when added to a DataSource

I'm trying to use a BindingList as a DataSource for a ListBox in C # WinForms, but whenever I try to add items to a BindingList , I get an ArgumentOutOfRangeException throw. The following code demonstrates the problem (suppose the form is with ListBox listBox1 ):

 BindingList<string> dataSource = new BindingList<string>(); listBox1.DataSource = dataSource; dataSource.Add("Test1"); // Exception, here. 

Note that if the DataSource already has elements in it, I do not get an exception:

 BindingList<string> dataSource = new BindingList<string>(); dataSource.Add("Test1"); listBox1.DataSource = dataSource; dataSource.Add("Test2"); // Appears to work correctly. 

I can work around the problem by setting the DataSource property to null before adding the item and then setting the DataSource again, but this seems like a hack, and I would like to be able to avoid this.

Is there a way (without a hack) to use an empty DataSource on a ListBox so that adding elements to it does not throw exceptions?

Edit : Stack Trace:

System.Windows.Forms.dll! System.Windows.Forms.ListBox.SelectedIndex.set (intermediate value) + 0x1ec bytes
System.Windows.Forms.dll! System.Windows.Forms.ListControl.DataManager_PositionChanged (object sender, System.EventArgs e) + 0x2e bytes
System.Windows.Forms.dll! System.Windows.Forms.CurrencyManager.OnPositionChanged (System.EventArgs e) + 0x39 bytes
System.Windows.Forms.dll! System.Windows.Forms.CurrencyManager.ChangeRecordState (intermediate newPosition, check bool, bool endCurrentEdit, bool firePositionChange, bool pullData) + 0x14f bytes
System.Windows.Forms.dll! System.Windows.Forms.CurrencyManager.List_ListChanged (object sender, System.ComponentModel.ListChangedEventArgs e) + 0x2e4 bytes
System.dll! System.ComponentModel.BindingList.OnListChanged (System.ComponentModel.ListChangedEventArgs e) + 0x17 bytes
System.dll! System.ComponentModel.BindingList.FireListChanged (System.ComponentModel.ListChangedType type, index int) + 0x35 bytes
System.dll! System.ComponentModel.BindingList.InsertItem (intermediate index, System._Canon item) + 0x3f bytes
mscorlib.dll! System.Collections.ObjectModel.Collection.Add (System._Canon item) + 0x76 bytes

+4
source share
3 answers

It turns out that everyone checked in the dialog box "Exceptions" (Debug-> Exceptions). Thus, an exception exists, but (quietly) is handled by the .Net infrastructure. Ongoing program execution displays expected results.

+2
source

Perhaps you have an event handler associated with some event on your ListBox that might trigger this? I cannot reproduce the behavior you described.

I created a completely empty WinForms project, with a single ListBox bound to a BindingList<string> , added the value "Test" to the list (after setting the ListBox.DataSource property), and the "Test" element appeared in the field, as expected.

I would look at your ListBox as well as your BindingList<string> to see if you have any connected event handlers that might be missing.

0
source

I had the same problem, and after several studies, I found that the only workaround to avoid this .Net error was to assign only a BindingList to the DataSource when the list is not empty.

If this can change, you can create a dummy object that you always keep in the list, and you deleted it when the list is not empty.

Finally, you should not look for a way to avoid an ArgumentOutOfRangeException.

0
source

Source: https://habr.com/ru/post/1332803/


All Articles