how to go to previous form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • subrat kumarnayak
    New Member
    • Jul 2010
    • 10

    #1

    how to go to previous form

    by clicking a button at form2 i went to form3 but again from form3 i wnt 2 go 2 frm2by clicking a button in frm3
  • fastestindian
    New Member
    • Aug 2009
    • 74

    #2
    I am not sure whether there is any inbuilt mechanism to get the previous form, If it is there then i dont know
    but it there is none then you can do following.

    1. Create a Form Type variable. Name it Current Form
    2. Whenver u create a new instance, you store CurrentForm instance to PreviousForm Insance and new insance inthe CurrentForm Reference.
    3. Whenever you want to open previous form you access PreviousForm.Sh ow() to show it.
    4. Create the funcationality using above logic.

    Best of luck...

    Comment

    • GaryTexmo
      Recognized Expert Top Contributor
      • Jul 2009
      • 1501

      #3
      What fastestindian suggested is spot on; however, an alternative for a cleaner interface is to use UserControls to populate a panel instead of separate forms.

      For example...

      Code:
      public class WizardPanel : UserControl { ... }
      Now you can create a bunch of WizardPanel objects and design them using designer to look like however you want.

      Code:
      public class MyPanel1 : WizardPanel { ... }
      public class MyPanel2 : WizardPanel { ... }
      public class MyPanel3 : WizardPanel { ... }
      Code:
      public class Wizard
      {
        private List<WizardPanel> m_panelList = new List<WizardPanel>();
        private int m_currentIndex = 0;
      
        public void Add(WizardPanel panelToAdd) { ... }
        public void Remove(WizardPanel panelToRemove { ... }
        public void Remove(int index) { ... }
        public WizardPanel Get(int index) { ... }
        public WizardPanel GetFirst() { ... }
        public WizardPanel GetNext() { ... }
        public WizardPanel GetLast() { ... }
        public WizardPanel GetPrev() { ... }
      }
      Create a Wizard object as a private member in your main form. Also throw two panels onto your form. Dock one to fill and the other to the bottom. Put a next/back button in the lower button.

      In your constructor, you can put...

      Code:
      myWizard.Add(new MyPanel1());
      myWizard.Add(new MyPanel2());
      myWizard.Add(new MyPanel3());
      
      mainPanel.Controls.Clear();
      mainPanel.Controls.Add(myWizard.GetFirst());
      Your next button's click event ...

      Code:
      mainPanel.Controls.Clear();
      mainPanel.Controls.Add(myWizard.GetNext());
      ... and the back button ...

      Code:
      mainPanel.Controls.Clear();
      mainPanel.Controls.Add(myWizard.GetPrev());
      This is just a stubbed out, bare bones version of a way to get the functionality you want inside a single form instead of instantiating new ones. It may not be what you need (as I said, I think fastestindian's answer is the best solution for what you described) but it's always fun to think about other ways to go about it :)

      Good luck!

      Comment

      Working...