tags:

views:

122

answers:

1

I have a form containing a checkedlistbox, I want to be able to do a simple comparison between the checked stauts before and after to see if there has been any change. I have a copy of the "before" version of the checkedlist box and I thought I could just compare the two CheckedItems but it always thinks they are different, I could iterate through the entire checkedlistbox but I thought there would be an easier way.

the comparison is simply :

Dim CurCheckedItems As CheckedListBox.CheckedItemCollection = ReportChList.CheckedItems
Dim OldCheckedItems As CheckedListBox.CheckedItemCollection = OldReportChList.CheckedItems

If OldCheckedItems Is CurCheckedItems Then
Else
...
End If

P.S. Is there a way to do IsNot

.net 1.1 framework

A: 

The Is operator checks to see if the reference is the same, not the contents of the reference. So when you make a copy of the collection CheckedItems and compare it to another copy of CheckedItems with Is, it always will be false because they refer to different collections. (You can accomplish IsNot this way: "not (OldCheckedItems Is CurCheckedItems)", but that may not help you in this case.)

You can tell whether the checked items are different by setting a flag in the ItemCheck event. You can check the item currently being changed with the corresponding item in the oldCheckedList and maintain a flag that tells whether the checked items are the same. If oldCheckedList has been saved from a previous instance of the application, you will have to do an item by item comparison to set the flag at startup.

xpda