Having Next/Pervious Button in desktop application, windows application.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SalimZaabi
    New Member
    • May 2009
    • 14

    #1

    Having Next/Pervious Button in desktop application, windows application.

    Hi everybody,

    Can anyone help me ?
    How do I move to the next Form and go back without having to open new Form in window application. I tried to use in Form1
    Form2 f2 = new Form2();
    f2.show();
    but the problem is that technique opens new Form and if I went to go back to Form1 to modify some info and get back again to Form2, I will have new window of Form2. For example, lets say that I have questioneer application and a each question in a Form by itself and want to change the previous answers without opening new Forms, what is the best techniqe? .
    I know that in web application the "Response.Redir ect" is used.

    So, is anyone can help me on how to have Next/Previous application?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Please read my reply #7 in this thread.

    Comment

    • cloud255
      Recognized Expert Contributor
      • Jun 2008
      • 427

      #3
      Hi,

      There are a couple of ways to approach this. You could look into creating a public static form array in your main application class (Program.cs by default) or you could create a wrapper class which each form inherits from, with the wrapper containing the array.

      This way your application is extensible as you can easily add and remove forms without having to change the code. Below is how the Next button might function:

      Code:
      if (MyApplication.Program.frmArr[currentFrm+1] != null)
                  {
                      MyApplication.Program.frmArr[currentFrm+1].Show();
                  }
                  else
                  {
                      MyApplication.Program.frmArr[currentFrm+1] = new Form();
                      MyApplication.Program.frmArr[currentFrm+1].Show();
                  }
      With this approach you only instantiate a new form if the next or previous form is NULL, that way your changes will not be lost.

      Hope this helps

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by cloud255
        Hi,

        There are a couple of ways to approach this. ...
        .. and the correct way depends on the program design if any. See my reply above.

        Comment

        • balame2004
          New Member
          • Mar 2008
          • 142

          #5
          You can not do it with forms. Instead you can use multiple panels in a form. Use each panel for each question and set the Dock property to be Fill(eg: Panel1.Dock=Doc kStyle.Fill;) for the panel that contains current question.

          Do the following steps:
          1.Add Windows Form.
          2.Add a panel(say PanelControls) at the bottom of the form(Set Dock property of the panel to be Bottom) and add prev and next buttons in this panel.
          3. Add panels that contain questions.
          4.Initially set(Set Dock property of panel to be Fill) a panel that contains question 1.
          5.Set appropriate panel(i.e. set Dock property of panel to be Fill) whenever you click prev and next buttons.

          Hope it will help you!

          Comment

          • cloud255
            Recognized Expert Contributor
            • Jun 2008
            • 427

            #6
            Originally posted by r035198x
            .. and the correct way depends on the program design if any. See my reply above.
            I agree with you that object design is largely neglected, I personally use MVP and do as little as possible on the forms themselves. But my experience is that many people don't bother to create any real design at all before development and this is a problem that should be addressed. But often we at the forum have no idea what the application as a whole should achieve so helping people to create proper designs is impossible.

            With a detailed diagram of what objects you will be using and how they interact, most of the problems encountered at these forums could be avoided but most people here are devs and not architects, so I try to provide people with easy to implement work-arounds to the problems they face.

            Maybe we should create an Analysis and Design section in Bytes, I really believe that alot of people would benefit from such a feature.

            Regards

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by cloud255
              ..
              Maybe we should create an Analysis and Design section in Bytes, I really believe that alot of people would benefit from such a feature.

              Regards
              Start a thread about it and let's see what others think of the idea.

              Comment

              • SalimZaabi
                New Member
                • May 2009
                • 14

                #8
                Thanks for everyone who replied to me ....

                Comment

                • adyanta2050
                  New Member
                  • Oct 2009
                  • 1

                  #9
                  Or how about passing 'this' into form constructor ?

                  Example:
                  Form1 has Next button which pops out Form2.
                  Form2 has Previous button which takes to Form1. (not instantiate. so data is persistent)
                  Form2 has Next button which may invoke Form3 and so on ...
                  Last form has Finish button which closes all forms.


                  Form1's next button click method:

                  private void btnNextform1_Cl ick(object sender, EventArgs e)
                  {
                  this.Hide();

                  Form2 f2 = new Form2(this);
                  f2.ShowDialog() ;

                  }



                  Form2's default constructor:

                  public partial class Form2 : Form
                  {
                  Form tmpfrm = null;

                  public Form2(Form1 parent)
                  {
                  InitializeCompo nent();
                  tmpfrm = parent;

                  }


                  Form2's Previous button click method:

                  private void btnPreviousForm 2_Click(object sender, EventArgs e)
                  {

                  tmpfrm.Visible = true;
                  tmpfrm.Show();

                  this.Close();

                  }


                  Assuming Form2 has a finish button, its method would be:

                  private void btnFinishForm2_ Click(object sender, EventArgs e)
                  {
                  tmpfrm.Close();
                  this.Close();
                  }

                  -x-

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    I would have suggested using a tabControl and setting the tabs to hidden (75% certain you can do that) and then cycling through the tabs.
                    The only reason I say that over using a Panel collection, is that at design time, it is easier to cycle between the panels to add the widgets.

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      You could do it with forms by taking control of the .Close() and having the form .Hide() instead. This way you make all your forms in advance and only .Show and .Hide as needed.

                      Though my preferences is to not have the content of the form be on a Form but instead in a user control. This way I can have one Form open, and drop in/out different User Controls

                      Other good suggestions have been made as well.

                      Its really just a matter of personal programming style preference.

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        As a side note, you can add a form to another form like you could a control, but its a little trickier then just using a usercontrol

                        Comment

                        Working...