M
Matthew
Hi,
I am using a checkedlistbox on a windows form and binding it to a
collection of classes.
clbAliases is the checkedlistbox control
selectedplace is a class with property placealiases.This property is a
collection of placealias classes. Hopefully the rest is
self-explanatory..
I bind the control as follows:
If SelectedPlace.PlaceAliases.Count > 0 Then
SetCLBAliasDataSource()
Else
clbAliases.DataSource = Nothing
End If
Private Sub SetCLBAliasDataSource()
clbAliases.DataSource = Nothing
clbAliases.DisplayMember = "AliasName"
clbAliases.DataSource = SelectedPlace.PlaceAliases
End Sub
Then when I modify the underlying collection ( eg for delete
functionality )
I have this:
Private Sub btnDeleteAlias_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnDeleteAlias.Click
If SelectedPlace.PlaceAliases.Count = 1 Then
MsgBox("You cannot delete the primary alias.")
Exit Sub
Else
Dim item As Object
For Each item In clbAliases.CheckedItems
'clbParents.Items.Remove(item)
SelectedPlace.PlaceAliases.Remove(item.placenameid)
Next
'this forces the control to rebind to the modified
collection
SetCLBAliasDataSource
End If
End Sub
Everything works as expected, EXCEPT once the item is removed, and the
user clicks into the checkedlistbox area , specifically on any
remaining item in the box, the following error occurs:
"An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative
and less than the size of the collection."
I have experimented with refreshing the checkedlistbox, and a number
of various other attempted workarounds without success.
The error occurs whether I specify a ValueMember property or not. When
I inspect the underlying collection in the locals window, it appears
normal, as does the items collection of the control. And both contain
the expected number of items. In addition the control correctly
displays the remaining items.
I can only assume that the control is referencing a non-existent
collection member somewhere in the click event event args. Execution
breaks on the line with the class definition :
Public Class GISMain
Can anyone point out a workaround, or my mistake??
Thanks in advance for any help..
Matthew Evans
I am using a checkedlistbox on a windows form and binding it to a
collection of classes.
clbAliases is the checkedlistbox control
selectedplace is a class with property placealiases.This property is a
collection of placealias classes. Hopefully the rest is
self-explanatory..
I bind the control as follows:
If SelectedPlace.PlaceAliases.Count > 0 Then
SetCLBAliasDataSource()
Else
clbAliases.DataSource = Nothing
End If
Private Sub SetCLBAliasDataSource()
clbAliases.DataSource = Nothing
clbAliases.DisplayMember = "AliasName"
clbAliases.DataSource = SelectedPlace.PlaceAliases
End Sub
Then when I modify the underlying collection ( eg for delete
functionality )
I have this:
Private Sub btnDeleteAlias_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles btnDeleteAlias.Click
If SelectedPlace.PlaceAliases.Count = 1 Then
MsgBox("You cannot delete the primary alias.")
Exit Sub
Else
Dim item As Object
For Each item In clbAliases.CheckedItems
'clbParents.Items.Remove(item)
SelectedPlace.PlaceAliases.Remove(item.placenameid)
Next
'this forces the control to rebind to the modified
collection
SetCLBAliasDataSource
End If
End Sub
Everything works as expected, EXCEPT once the item is removed, and the
user clicks into the checkedlistbox area , specifically on any
remaining item in the box, the following error occurs:
"An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in mscorlib.dll
Additional information: Index was out of range. Must be non-negative
and less than the size of the collection."
I have experimented with refreshing the checkedlistbox, and a number
of various other attempted workarounds without success.
The error occurs whether I specify a ValueMember property or not. When
I inspect the underlying collection in the locals window, it appears
normal, as does the items collection of the control. And both contain
the expected number of items. In addition the control correctly
displays the remaining items.
I can only assume that the control is referencing a non-existent
collection member somewhere in the click event event args. Execution
breaks on the line with the class definition :
Public Class GISMain
Can anyone point out a workaround, or my mistake??
Thanks in advance for any help..
Matthew Evans