I have a two-form VB.NET project and I am not using MDI for either form. I merely want to be able to go to Form2 from a click event on Form1. When this happens, I want Form1 to close! So my simple code would be:
By submitting your email address, you agree to receive emails regarding relevant topic offers from TechTarget and its partners. You can withdraw your consent at any time. Contact TechTarget at 275 Grove Street, Newton, MA.
dim Form2 as new Form2 Form2.Show() Me.Close()
Doing this however shuts down BOTH forms? Thanks.
Before I answer this, I just want to point out that in nearly all cases, applications that are switching forms on the user is not a good design. For one, it's confusing to the user, and as you can tell, it really isn't something that's supported by the .NET Framework (or any framework for that matter). However, I do not know the particulars of your application, so you may have a case where this makes perfect sense.
By calling Me.Close() on Form1, you are telling the application to end its main thread, which ends the entire application. What you'll need to do is call Me.Hide() instead to hide the main form and let Form2 continue running as a modaless window. The problem is that when you close Form2, your application will continue to run because there's no automatic way to have the main form end when all modaless windows are closed. What you'll need to do is implement a way to pass an instance of Form1 to Form2. When Form2's Close method, which you'll need to override, is called, you can call Form1's close method.
Dig Deeper on Win Development Resources
Related Q&A from John Robbins
What is a JAD session? This term is referred to often in job postings and seems like some kind of brainstorm you carry out with a client to define IT...continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.