• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Using C#, is there a way to close one form when another form is opened?

we are doing a project that requires multiple forms, however we cannot figure out how to close one form when another form opens. ifs there any tips or code you could show us to allow this to occur?

MIKE
 
From the predessor of C#, one would look for the window (by title or class) one wants to close and issue a close command to it when processing the command sequence to open the new window.

If the use of forms is similar to a Window, try it.
 
Edit ... Didn't read it close enough. NM.

You'll need some central place to -know- where/who the forms are - a collection of some type. Maybe make it static for your form class. In the open method, have it scan that collection, and close the appropriate form, if it exists.


Old answer (incorrect):

Add an event handler to the formclosing or formclosed event, and instruct it to create and show a new form ... does that work?

<pseudo code>

function form1_closed(Object sender, EventArgs e ) {
new form1().Show();
}

</pseudo code>

Also, when you run the program, don't use the default main they provide. The main thread will crap out on you after the first form is closed. Instead, tell the first form to show, and the Application.Run(). However, you'll need to manage -when- to issue the exit command then.
 
Originally posted by: nourdmrolNMT1
tried it and it doesnt like that, it likes to close the whole program.

MIKE

The main form you are using apparently is tied to the controlling logic of the application.

Seperate the UI from the initial application logic.
Create a dummy Form/Window to act as a messaging interface.

Then create your Forms/Windows as needed.


Again my advice is coming from a non-C# person, however, I have a strong understanding of the underlying Windows APIs

 
Back
Top