none
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll

    Question

  • I have an error with this code:

    Friend WithEvents Formm As New Form1 ' line with error
    
        Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
            Formm.Show()
            Me.WindowState = FormWindowState.Normal
            Formm.WindowState = FormWindowState.Normal
            Formm.Size = New System.Drawing.Size(New System.Drawing.Point(1036, 390))
            Me.Size = New System.Drawing.Size(New System.Drawing.Point(1036, 390))
            Formm.Left += 390
            Button19.Enabled = False
            AddHandler Formm.Closed, AddressOf Formm_Closed
        End Sub
        Private Sub Formm_Closed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Formm.Closed
            Button19.Enabled = True
        End Sub

    It only has the Formm as new form 1 part hilighted.

    Here's a picture

    [IMG]http://i1168.photobucket.com/albums/r490/poopsiedoodle707/daterrordoe_zps9d5f67e6.png[/IMG]


    Nathaniel Washburn N-Soft Developer

    Friday, January 25, 2013 11:54 PM

Answers

  • Your Form1 class is creating a new Form1 - this in turn, creates its own Form1, which creates another, etc. 

    This Form keeps recursively creating itself until your stack overflows, hence the exception.

    You shouldn't need to put the form in there - If you're clicking the button, you're already showing yourself - so there is no reason to have the form.  That being said, if you're trying to open a new copy of yourself, just move the declaration into your method:

    ' Don't have Formm here...
    
        Private Sub Button19_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button19.Click
            Dim Formm As New Form1
            Me.WindowState = FormWindowState.Normal
            Formm.WindowState = FormWindowState.Normal
            Formm.Size = New System.Drawing.Size(New System.Drawing.Point(1036, 390))
            Me.Size = New System.Drawing.Size(New System.Drawing.Point(1036, 390))
            Formm.Left += 390
            Button19.Enabled = False
            AddHandler Formm.Closed, AddressOf Formm_Closed
            Formm.Show()
        End Sub
    
        Private Sub Formm_Closed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
            Button19.Enabled = True
        End Sub


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    • Proposed as answer by CRM HUNT Saturday, January 26, 2013 9:11 AM
    • Marked as answer by Shanks ZenModerator Monday, February 04, 2013 6:33 AM
    Saturday, January 26, 2013 12:23 AM