# How can I load another form containing a new level when my player reaches the current level's exit?

I am coding a very basic side-scrolling game in C#, using Windows Forms.

I want to connect my game's levels with 'exit points'. When the player touches an exit point, a new form should be loaded immediately. How can I do this?

• It would be better if you would unload your level from the form then load the new level on that, instead of creating a new window. – János Turánszki Jan 14 '15 at 19:48
• Good luck! I actually remember my first platformer used PictureBoxes in Visual Basic.NET. Thinking back to how it was put together, I'm kind of amazed it worked as well as it did. – Katana314 Feb 13 '15 at 21:20

Yes, it's feasible: if the player hits the trigger area, create the new form and position it appropriately and then close the old form (the order is relatively important here, because the default Windows Forms application driver, the Run call, will terminate the application when the last form is closed so you always want to make sure at least one exists, or you want to change that Run call).

For example, assuming you call a method of the current form called OnPlayerHitExit when the player hits the exit area, you could:

void OnPlayerHitExit () {
var nextLevel = new FormContainingNextLevel();
nextLevel.Show();
Close();
}


That said, you shouldn't put each level in its own form. A form is a Win32 window, and closing one/opening a new one will be visible and noticeable to the player even if you spend time doing all the necessary hackery to get as close as possible to a seemless transition.

Instead, store your level data independently of the form itself (maybe in a Level class) and when the player hits the trigger area, replacing the existing level data with new level data.