Results 1 to 2 of 2

Thread: stack overflow

  1. #1

    Thread Starter
    Hyperactive Member Grunt's Avatar
    Join Date
    Oct 2004
    Location
    Las Vegas
    Posts
    499

    stack overflow

    I have the following items in a checklist. I am using 2005:

    Code:
    Community
    Community-->Activities
    Community-->Artists
    Community-->Childcare
    Community-->General
    Community-->Groups
    Community-->Pets
    Community-->Events
    Community-->Lost+Found
    Community-->Musicians
    Community-->Local New
    Community-->Politics
    Community-->Rideshare
    Community-->Volunteers
    Housing
    Housing-->Apts/Housing
    Housing-->Rooms/Shared
    Housing-->Sublets/Temporary
    Housing-->Housing Wanted
    Housing-->Housing Swap
    Housing-->Vacation Rental
    Housing-->Parking/Storage
    Housing-->Office/Commercial
    Housing-->Real Estate for Sale
    what I want is if someone clicks "Housing", everything starting with housing is checked, or if the user clicks "Community everything starting with community is checked. And vice versa if it is unchecked.

    I tried this but it results in a stack overflow on SetItemChecked:
    Code:
    Private Sub chkCategories_ItemCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemCheckEventArgs) Handles chkCategories.ItemCheck
            'loop through the checklist
            If Me.chkCategories.SelectedItem = "Community" Then
                For i As Integer = 0 To Me.chkCategories.Items.Count - 1
                    If Me.chkCategories.Items.Item(i).ToString.StartsWith("Community") = True Then
                        Me.chkCategories.SetItemChecked(i, True)
                    End If
                Next
            End If
        End Sub
    End Class
    any ideas on how i can accomplish this?

  2. #2
    Frenzied Member
    Join Date
    Jun 2005
    Posts
    1,950

    Re: stack overflow

    I think you get the overflow because you are invoking this same event every time you check an item within the event.

    You need to inhibit the event when it is first executed, to prevent further executions as you loop.

    As a quick test just to confirm this is the cause, try modifying the code like this...

    Code:
    Dim Inhibit As Boolean = False
    
    Private Sub chkCategories_ItemCheck(_
    ByVal sender As Object, _
    ByVal e As System.Windows.Forms.ItemCheckEventArgs) _
    Handles chkCategories.ItemCheck
    
    If Not Inhibit Then
            Inhibit = True
    
            'loop through the checklist
            If Me.chkCategories.SelectedItem = "Community" Then
                For i As Integer = 0 To Me.chkCategories.Items.Count - 1
                    If Me.chkCategories.Items.Item(i).ToString.StartsWith("Community") Then
                        Me.chkCategories.SetItemChecked(i, True)
                    End If
                Next
            End If
    
            Inhibit = False
    End If
    
        End Sub
    End Class

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