-
Jul 22nd, 2024, 02:38 PM
#1
Thread Starter
New Member
[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?
-
Jul 22nd, 2024, 03:56 PM
#2
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.
-
Jul 22nd, 2024, 04:37 PM
#3
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:
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.
-
Jul 22nd, 2024, 10:53 PM
#4
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:
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|