CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2003
    Posts
    4

    Angry Changing checkbox state of listviewitem form code

    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.

  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868
    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

  3. #3
    Join Date
    Feb 2003
    Posts
    4
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured