Displaying new forms.

I want to open 'Form2' from 'Form1'.
My manual informs me that I need to create a new instance for both forms and must be declared outside any procedure otherwise many instances of the new form will be created.

So I tried:

Public Class Form1
Inherits System.Windows.Forms.Form

Dim frmForm1 As New Form1()
Dim frmForm2 As New Form2()

Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e
As System.EventArgs)Handles Button1.Click

frmForm2.Show()

End Sub


When I execute this I get a 'System.StackOverflowException' error.

But if I use:

Public Class Form1
Inherits System.Windows.Forms.Form

Dim frmForm1 As New Form1()

Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e
As System.EventArgs)Handles Button1.Click

Dim frmForm2 As New Form2()
frmForm2.Show()

End Sub

I do not get a Stack Overflow error but, as previously mentioned, I am able to create many instances of the same form which I do not want to do.
Also when I close the form using the close 'X' button I am unable to reopen it and this also produces a runtime error telling me that I cannot open an object that has been closed.

This was so easy to do in VB6.

Any Ideas???

Thanks in advance.



Comments

  • : I want to open 'Form2' from 'Form1'.
    : My manual informs me that I need to create a new instance for both forms and must be declared outside any procedure otherwise many instances of the new form will be created.
    :
    : So I tried:
    :
    : Public Class Form1
    : Inherits System.Windows.Forms.Form
    :
    : Dim frmForm1 As New Form1()
    : Dim frmForm2 As New Form2()
    :
    : Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e
    : As System.EventArgs)Handles Button1.Click
    :
    : frmForm2.Show()
    :
    : End Sub
    :
    :
    : When I execute this I get a 'System.StackOverflowException' error.
    :
    : But if I use:
    :
    : Public Class Form1
    : Inherits System.Windows.Forms.Form
    :
    : Dim frmForm1 As New Form1()
    :
    : Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e
    : As System.EventArgs)Handles Button1.Click
    :
    : Dim frmForm2 As New Form2()
    : frmForm2.Show()
    :
    : End Sub
    :
    : I do not get a Stack Overflow error but, as previously mentioned, I am able to create many instances of the same form which I do not want to do.
    : Also when I close the form using the close 'X' button I am unable to reopen it and this also produces a runtime error telling me that I cannot open an object that has been closed.
    :
    : This was so easy to do in VB6.
    :
    : Any Ideas???
    :
    : Thanks in advance.
    :
    :
    :
    :

    I agree they took an excellent RAD tool and managed to make it a pain to deal with. If I wanted to learn a whole new Obj Model I would have adopted C#.
    Anyway, You don't have want to create a Form 1 inside Form 1. For what you wanted to do:

    Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim frmForms2 as New Form2()

    Windows Form Designer ...

    Private Sub Button1_Click(...)
    frmForms2.Show()
    End Sub
    End Class
  • : : I want to open 'Form2' from 'Form1'.
    : : My manual informs me that I need to create a new instance for both forms and must be declared outside any procedure otherwise many instances of the new form will be created.
    : :
    : : So I tried:
    : :
    : : Public Class Form1
    : : Inherits System.Windows.Forms.Form
    : :
    : : Dim frmForm1 As New Form1()
    : : Dim frmForm2 As New Form2()
    : :
    : : Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e
    : : As System.EventArgs)Handles Button1.Click
    : :
    : : frmForm2.Show()
    : :
    : : End Sub
    : :
    : :
    : : When I execute this I get a 'System.StackOverflowException' error.
    : :
    : : But if I use:
    : :
    : : Public Class Form1
    : : Inherits System.Windows.Forms.Form
    : :
    : : Dim frmForm1 As New Form1()
    : :
    : : Private Sub Button1_Click(ByVal Sender As System.Object, ByVal e
    : : As System.EventArgs)Handles Button1.Click
    : :
    : : Dim frmForm2 As New Form2()
    : : frmForm2.Show()
    : :
    : : End Sub
    : :
    : : I do not get a Stack Overflow error but, as previously mentioned, I am able to create many instances of the same form which I do not want to do.
    : : Also when I close the form using the close 'X' button I am unable to reopen it and this also produces a runtime error telling me that I cannot open an object that has been closed.
    : :
    : : This was so easy to do in VB6.
    : :
    : : Any Ideas???
    : :
    : : Thanks in advance.
    : :
    : :
    : :
    : :
    :
    : I agree they took an excellent RAD tool and managed to make it a pain to deal with. If I wanted to learn a whole new Obj Model I would have adopted C#.
    : Anyway, You don't have want to create a Form 1 inside Form 1. For what you wanted to do:
    :
    : Public Class Form1
    : Inherits System.Windows.Forms.Form
    : Dim frmForms2 as New Form2()
    :
    : Windows Form Designer ...
    :
    : Private Sub Button1_Click(...)
    : frmForms2.Show()
    : End Sub
    : End Class
    :

    Thanks for the reply.
    This works fine except now when I close the second form using the 'Close' button ('X') and then try to reopen it I get a 'System.ObjectDisposedException' error.
    I understand what this is but i'm not sure how to get around it.

    Cheers.


  • WE have the problem too... we beleive it is because you are creating an instance of a form 2 technically... it doesn't make sence... what we do is we HIDE the orginal form and right before we close the second form we show a NEW instance of the orignal. This will get rid of the problem, but there will be multiple instance of the same form open and hidden when all is said and done. When you are done with the form make your exit button just have the keyword End in it. That will stop to program and the debugging.



    : Thanks for the reply.
    : This works fine except now when I close the second form using the 'Close' button ('X') and then try to reopen it I get a 'System.ObjectDisposedException' error.
    : I understand what this is but i'm not sure how to get around it.
    :
    : Cheers.
    :
    :
    :

  • : WE have the problem too... we beleive it is because you are creating an instance of a form 2 technically... it doesn't make sence... what we do is we HIDE the orginal form and right before we close the second form we show a NEW instance of the orignal. This will get rid of the problem, but there will be multiple instance of the same form open and hidden when all is said and done. When you are done with the form make your exit button just have the keyword End in it. That will stop to program and the debugging.
    :
    :
    :
    : : Thanks for the reply.
    : : This works fine except now when I close the second form using the 'Close' button ('X') and then try to reopen it I get a 'System.ObjectDisposedException' error.
    : : I understand what this is but i'm not sure how to get around it.
    : :
    : : Cheers.
    : :
    : :
    : :
    :
    :


    Dont use form2.show, use form2.showdialog(form2 would be the variable name) This should solve your problem.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories