How to Load Form2 from Form1
-
Another CPian wrote to me in a separate email, asking "...trying to have a form, that when you click on a command button, the main form comes up. The first one would be an opening form, and the second the main program" I thought I'd post the first solution that occurred to me here, and ask if anyone else has a brighter idea. Here's what I did. I create two forms, one named StartupForm, and one named MainForm. The form StartupForm is set as the startup object in the project's properties. StartupForm:
Public Class StartupForm
Inherits System.Windows.Forms.Form' Windows Form Designer generated code omitted Private Sub cmdStart\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click Dim myForm As New MainForm(Me) ' MainForm's constructor New is overloaded Me.Hide() ' Make me invisible myForm.Show() ' Display MainForm End Sub
End Class
MainForm:
Public Class MainForm
Inherits System.Windows.Forms.FormPrivate \_caller As Object ' Windows Form Designer generated code omitted Public Sub New(ByVal caller As Form) MyBase.New() InitializeComponent() \_caller = caller End Sub Private Sub cmdClose\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click CType(\_caller, Form).Close() Me.Close() End Sub
End Class
This was pretty quick & dirty, so there are probably some obvious enhancements to be made. Suggestions, comments, questions and revisions are invited...
-
Another CPian wrote to me in a separate email, asking "...trying to have a form, that when you click on a command button, the main form comes up. The first one would be an opening form, and the second the main program" I thought I'd post the first solution that occurred to me here, and ask if anyone else has a brighter idea. Here's what I did. I create two forms, one named StartupForm, and one named MainForm. The form StartupForm is set as the startup object in the project's properties. StartupForm:
Public Class StartupForm
Inherits System.Windows.Forms.Form' Windows Form Designer generated code omitted Private Sub cmdStart\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStart.Click Dim myForm As New MainForm(Me) ' MainForm's constructor New is overloaded Me.Hide() ' Make me invisible myForm.Show() ' Display MainForm End Sub
End Class
MainForm:
Public Class MainForm
Inherits System.Windows.Forms.FormPrivate \_caller As Object ' Windows Form Designer generated code omitted Public Sub New(ByVal caller As Form) MyBase.New() InitializeComponent() \_caller = caller End Sub Private Sub cmdClose\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click CType(\_caller, Form).Close() Me.Close() End Sub
End Class
This was pretty quick & dirty, so there are probably some obvious enhancements to be made. Suggestions, comments, questions and revisions are invited...
I just thought of another way to implement the splash form by showing the SplashForm on the Load() event of the MainForm and hiding the MainForm. Public Class MainForm Inherits System.Windows.Forms.Form Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim splashForm As New SplashForm() splashForm.ShowDialog(Me) Me.Hide() End Sub End Class Public Class SplashForm Inherits System.Windows.Forms.Form Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click Me.Close(); Me.Owner.Show(); End Sub End Class I have tested it and it seems to be working fine. Edbert P. Sydney, Australia.
-
I just thought of another way to implement the splash form by showing the SplashForm on the Load() event of the MainForm and hiding the MainForm. Public Class MainForm Inherits System.Windows.Forms.Form Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim splashForm As New SplashForm() splashForm.ShowDialog(Me) Me.Hide() End Sub End Class Public Class SplashForm Inherits System.Windows.Forms.Form Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click Me.Close(); Me.Owner.Show(); End Sub End Class I have tested it and it seems to be working fine. Edbert P. Sydney, Australia.
-
I like it. My only thought is that the other fellow was asking about two forms, neither of which sounded like a splash form when he described them to me. Does Form1 flash on the screen at all before it hides itself?
-
When I tested it Form1 didn't flash at all. I suppose since I implemented the code in the Form_Load event it hadn't showed the form itself before the code got triggered. Edbert P. Sydney, Australia.