Click to See Complete Forum and Search --> : Changing checkbox state of listviewitem form code


hinge
February 5th, 2003, 02:33 PM
Task:
I have a listview with checkboxes. The idear is that the only one item in the listview can be checked at one time. So when the user clicks on a new item the all other items should be unchecked (the other item).

My solution:
in the listview1_ItemCheck event I put code to uncheck the old item, ei:

Olditem.checked = CheckState.Unchecked

Problem:
this line of code

Olditem.checked = CheckState.Unchecked

makes the listview1_ItemCheck event fire again and thus causing an endless loop.....

Somebody.............Please help, this is not how it was in VB6...I know that some of you hate the remark.

DSJ
February 5th, 2003, 03:07 PM
Try this:

Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
If e.NewValue = CheckState.Checked Then
If Not oldItem Is Nothing Then
oldItem.Checked = False
End If
oldItem = ListView1.Items(e.Index)
Else
'Nothing
End If
End Sub

hinge
February 5th, 2003, 03:21 PM
It did the job...Cheers!!

Originally posted by DSJ
Try this:

Private Sub ListView1_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles ListView1.ItemCheck
If e.NewValue = CheckState.Checked Then
If Not oldItem Is Nothing Then
oldItem.Checked = False
End If
oldItem = ListView1.Items(e.Index)
Else
'Nothing
End If
End Sub