Results 1 to 4 of 4

Thread: Closing a Form Error

  1. #1

    Thread Starter
    Junior Member
    Join Date
    May 2017
    Posts
    27

    Closing a Form Error

    Form should close when either the cmdCancel command button or the red X button is clicked.

    The code below works fine but a System.StackOverflowException error occurs if the same code is also declared in the Me.FormClosing event.

    Code:
        Private Sub cmdCancel_Click(sender As Object, e As EventArgs) Handles cmdCancel.Click
            If NotEmptyChecker(grpUserInfo) = 0 Then                                                            '***All fields are empty***
                Me.Close()
            ElseIf NotEmptyChecker(grpUserInfo) > 0 Then                                                            '***Atleast 1 field is not empty***
                If MsgClearFilledFields(grpUserInfo) = True Then
                    Me.Close()
                End If
            Else
                Me.Close()
            End If
        End Sub
    Is there a problem with my Global Function?
    Code:
        Public Function NotEmptyChecker(ByVal group As Control) As Integer
            'Public Function NotEmptyChecker(ByVal group As Control, ByVal label As Control) As Integer
            ctrlChecker = group.Controls.OfType(Of TextBox).Count + group.Controls.OfType(Of ComboBox).Count
            For Each ctrl As Control In group.Controls
                If TypeOf ctrl Is TextBox Then
                    If Len(ctrl.Text) = 0 Then
                        ctrlChecker += (-1)
                    End If
                ElseIf TypeOf ctrl Is ComboBox Then
                    If Len(ctrl.Text) = 0 Then
                        ctrlChecker += (-1)
                    End If
                End If
            Next
            notEmptyFields = ctrlChecker
            Return notEmptyFields
        End Function

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Closing a Form Error

    I think your function is fine. You have an infinite recursion when you run the code from the close handler because you are calling Me.Close from it. You are essentially raising an event from the event handler.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3

    Thread Starter
    Junior Member
    Join Date
    May 2017
    Posts
    27

    Re: Closing a Form Error

    Any suggestion on how i should close the form? I can't use End or Exit since this is a MDI Child form.

  4. #4
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,757

    Re: Closing a Form Error

    You close the form just as you are doing however, if you want to run that code in the close event handler then you need to remove the calls to Me.Close. That's where the error is coming from.

    Now if during the close handler the conditions of your form dictate that you should not close the form, then set e.Cancel= True and the form will stay open.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

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