CheckedListBox crashes after complex datasource is modified

  • Thread starter Thread starter Matthew
  • Start date Start date
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 don't have the answer for you but I have an advice: Stay away from
databound CheckedListBoxes.. lots of bugs there (at least on version 1.0).
If I were you I'd just populate and mark the items manually. You'll notice
that the databound Checkedlistbox does not keep its state correctly, it
likes to forget which items were selected every now and then.
 
TJoker .NET said:
I don't have the answer for you but I have an advice: Stay away from
databound CheckedListBoxes.. lots of bugs there (at least on version 1.0).
If I were you I'd just populate and mark the items manually. You'll notice
that the databound Checkedlistbox does not keep its state correctly, it
likes to forget which items were selected every now and then.

--
TJoker, MCSD.NET
MVP: Paint, Notepad, Solitaire

****************************************


Thanks, I had the impression it was a bug. How irritating that the
complex databinding functionality is implemented with bugs. I guess I
need to give 1.1 a go.
 
Thanks, I had the impression it was a bug. How irritating that the
complex databinding functionality is implemented with bugs. I guess I
need to give 1.1 a go.


Having spent half the day installing fwork v1.1 and visual studio
2003, I now find that the behaviour is identical. Does anyone else
have any ideas?

thanks again,

Matthew
 
Thanks, I had the impression it was a bug. How irritating that the
complex databinding functionality is implemented with bugs. I guess I
need to give 1.1 a go.

Uh....no.. the error message was as above:

its now this:

An unhandled exception of type 'System.ArgumentOutOfRangeException'
occurred in microsoft.visualbasic.dll

Additional information: Specified argument was out of the range of
valid values.
 
Back
Top