Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I've started working on a project that is basically a step by step wizard for creating a set of files for another program.

My form contains a panel that I have created a class for: MainPanel. MainPanel basically just sets the Size and Location. Each step used in the process is represented by a class that inherits from MainPanel. When the user clicks "Next" or "Back", I want to get rid of the current panel, and switch to the appropriate one.

I save all the data along the way in a Results class, so if necessary I can make a constructor to repopulate all the data on a "Back" click.

So my question is, what is the best way to get rid of the old panel and show the next one?

EDIT:
I'll leave a sample of some code with what I was thinking about doing. It works, but it seems like there's probably a better way to do it...

class MainForm
{
   MainPanel mainPanel;

   enum screens { FIRST, SECOND, THIRD };

   screens currentScreen;

   MainForm()
   {
      mainPanel = new firstPanel();
      this.Controls.Add(mainPanel);
      currentScreen = screens.FIRST;
   }

   void nextButton_Click(object sender, EventArgs e)
   {
      mainPanel.dispose();
   }

   void mainPanel_Disposed(object sender, EventArgs e)
   {
      // i have an enum, screens, (for now) to keep track of the screen i'm on
      if(currentScreen.Equals(screens.FIRST))
      {
         mainPanel = new secondPanel();
         this.Controls.Add(mainPanel);
         currentScreen = screens.SECOND;
      }
      // ...
   }
}


Still looking for more input, links and examples are greatly appreciated.
Posted
Updated 27-Jun-12 9:04am
v5

1 solution

Personally, unless there are a very large number of panels, I wouldn't dispose of each one. I would just set the visibility of the one shown to false and the next/last one to be shown to true. Much simpler and handling the data caching is easier too as it will still be in the no longer visible controls.
 
Share this answer
 
Comments
obp42 27-Jun-12 9:12am    
So you're suggesting that I don't use polymorphism on mainPanel, but instead create a new instance of FirstPanel, Second Panel, etc.? I'll try it out, but it's possible that a couple of the screens will have a lot of data on them - enough that I believe it could definitely affect performance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900