With .NET 1.1 / Visual Studio 2003 having been out for a good 4 years and already service packed by Microsoft once, its not every day that you come across a bug that really messes things up for a developer. This one however is definately one of those bugs that throws a developer off track and puzzles the heck out of them.
For example, lets say we have a ComboBox called ComboBox1 and a ListView called ListView1, both of which contain a range of items 1 through 20 on their list of items. Also, suppose you have the "MultiSelect" property of the ListView set to "False". Now lets say we want to select the same item on the combo box, everytime an item is selected in the ListView. Our
SelectedIndexChanged Event may look something like this:
Dim lvItem As New ListViewItem
lvItem = ListView1.SelectedItems.Item(0)
ComboBox1.SelectedItem = lvItem.SubItems(0).Text
Which would be a valid way to acomplish this task. If you however run the above code, you will see the following error message:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in system.windows.forms.dll
Additional information: Specified argument was out of the range of valid values.
If you put a break on the last line of the code and step through the code, you will notice that the "SelectedIndexChanged" event fires not once but TWICE! The reason for this is that .NET 1.1 does NOT clear the Selected Item in the collection after the event fires the first time, but it really should! So a simple way to fix this error would be to change your code to the following instead (to force the Selected Item to clear after the event is fired):
lvItem = ListView1.SelectedItems.Item(0)
ComboBox1.SelectedItem = lvItem.SubItems(0).Text
ListView1.SelectedItems.Clear()
Another way to accomplish the same thing (without clearing the list) would be to do the following:
If ListView1.SelectedItems.Count > 0 Then
Dim lvItem As New ListViewItem
lvItem = ListView1.SelectedItems.Item(0)
ComboBox1.SelectedItem = lvItem.SubItems(0).Text
End If
This bug HAS been fixed in .NET 2.0 or higher, and is only true for .NET 1.0 or 1.1.
Pete Soheil
DigiOz Multimedia
http://www.digioz.com
No comments:
Post a Comment