By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,582 Members | 1,694 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,582 IT Pros & Developers. It's quick & easy.

CheckedListBox crashes after complex datasource is modified

P: n/a
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
Nov 20 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

****************************************
"Matthew" <ma****************@hotmail.com> wrote in message
news:4f**************************@posting.google.c om...
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

Nov 20 '05 #2

P: n/a
"TJoker .NET [MVP]" <no****@nonono.no> wrote in message news:<up**************@TK2MSFTNGP10.phx.gbl>...
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.
Nov 20 '05 #3

P: n/a
ma****************@hotmail.com (Matthew) wrote in message news:<4f**************************@posting.google. com>...
"TJoker .NET [MVP]" <no****@nonono.no> wrote in message news:<up**************@TK2MSFTNGP10.phx.gbl>...
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.

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
Nov 20 '05 #4

P: n/a
ma****************@hotmail.com (Matthew) wrote in message news:<4f**************************@posting.google. com>...
"TJoker .NET [MVP]" <no****@nonono.no> wrote in message news:<up**************@TK2MSFTNGP10.phx.gbl>...
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.


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.
Nov 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.