Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. How to Load Form2 from Form1

How to Load Form2 from Form1

Scheduled Pinned Locked Moved Visual Basic
tutorial
5 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    John Kuhn
    wrote on last edited by
    #1

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

    Private \_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...

    E 1 Reply Last reply
    0
    • J John Kuhn

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

      Private \_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...

      E Offline
      E Offline
      Edbert P
      wrote on last edited by
      #2

      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.

      J 1 Reply Last reply
      0
      • E Edbert P

        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.

        J Offline
        J Offline
        John Kuhn
        wrote on last edited by
        #3

        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?

        E 1 Reply Last reply
        0
        • J John Kuhn

          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?

          E Offline
          E Offline
          Edbert P
          wrote on last edited by
          #4

          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.

          J 1 Reply Last reply
          0
          • E Edbert P

            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.

            J Offline
            J Offline
            John Kuhn
            wrote on last edited by
            #5

            That thought didn't dawn on me as soon as it should have... :doh:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups