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
: 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
: : 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.
: 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.
:
:
:
:
:
:
: : 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.