Results 1 to 4 of 4

Thread: [RESOLVED] How to navigate between multiple forms

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    7

    Resolved [RESOLVED] How to navigate between multiple forms

    I am working in VB2022. I am planning a program that will have three activities initiated by their respective button click on the main form (form1). I plan to handle each activity using a different form (form2, form3, and form4).
    How do I switch to form2 and then after the activity switch back to form1?

  2. #2
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,242

    Re: How to navigate between multiple forms

    To switch to the desired form, use the Button Click Event,

    Code:
            Using frm As New Form2
                frm.ShowDialog()
            End Using
    When your done with the for use the Close method and it will return to form1.

  3. #3
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,961

    Re: How to navigate between multiple forms

    Keep in mind that there is also the concept of default form instances. What wes4dbt shows you is how to instantiate a new form and show it as a dialog (aka modal) which is how I prefer to handle things.

    But in VB.NET, default form instances are instance of forms that are generated for you under the hood, which allow you to refer to a form by its type name without explicitly creating a new instance. For example, you could drop this in without doing what wes4dbt showed you and it'd work:
    Code:
    Form2.Show()
    This is useful when creating simple applications where you only need a single instance of each form and don't have to really the manage form states. Just keep in mind that for more complex situations, I would recommend explicitly creating and managing form instances. But if this is just a simple application that falls under "rapid application development" then using the default form instances may just work for you.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,665

    Re: How to navigate between multiple forms

    In case it's not clear, you can call Show or ShowDialog on either a default instance or an explicitly-created instance. Either is an instance so you can access any member of that type using either. Note that ShowDialog creates a modal dialogue, i.e. it won't allow you access to the calling form while it's open. If you call Show, you can access both forms. It sounds like a modal dialogue is what you want. That means that, if you wanted to, you could do this:
    Code:
    Form2.ShowDialog()
    Default instances are convenient and they make certain things easier for beginners, but they can also lead to confusion when you inevitably need to start creating your own instances. Default instances are never needed and exist primarily to allow VB.NET to behave like VB6 where forms are concerned, to stop certain people complaining about how VB.NET doesn't work the way they want. I would suggest learning to live without them as soon as possible, so it would help to understand them as well as possible. To that end, you may like to read this.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width