Retaining user input in Dialg-based application

nauman_ahmed
nauman_ahmed used Ask the Experts™
on
Hi,

I am creating a dialog-based application consisting on three forms. What I want is when the user click the back button, he will be able to see the inputs in the previous dialog. How is it possible? Currently I am using the following code for the Bacl button.

Form frmStepx = new FormName();
frmStepx.Show();

The problem is that the form is reloaded and user has to continue from the beginning.

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Create the forms ahead of time...

Form frmStep1 = new FormName1();
Form frmStep2 = new FormName2();
Form frmStep3 = new FormName3();

Then inside the function you're using...

int iCurrStep = 1;

while( true )
{
   DialogResult dlgResult = DialogResult.None;

   switch( iCurrStep )
   {
      case 1:   dlgResult = frmStep1.ShowDialog( );   break;
      case 2:   dlgResult = frmStep2.ShowDialog( );   break;
      case 3:   dlgResult = frmStep3.ShowDialog( );   break;
   }

   switch( dlgResult )
   {
      case DialogResult.OK:

         if( ++ iCurrStep == 4 )
            return true;

         break;

     case DialogResult.Retry:
        iCurrStep = Math.Max( 0, iCurrStep - 1 );
        break;

     case DialogResult.Cancel:
         return false;
   }
}

Author

Commented:
Thanks psdavis but I still have some confusion....

I don't have DialogResult.Retry and DialogResult.Cancel in my application. If you have workied in Visual Studion 6.0 C++ dialog based application, you may have noticed that it ask several thing before you finih your project. You are allowed to go back and change the options that you have selected. In my application, I am trying to do the same; actually this is a kind of wizard application. Following is the requirement:

- There can be more than 1 forms
- User must be able to go back and change the options that he has selected before he completes the wizard

What I couldn't do is when I go back, the options that I select in the previous form get lost. How can I make this work?

Author

Commented:
I got a solution, but need some feedback if it's good:

For every form, I set a button for the DialogResult = Cancel. In the parent form when the user click next, I use this code for n forms:

I initialize the formn in the constructor of each form.
Formn frmn = new Formn();

In the Next button click, I added this code:

this.Hide();
if (frmn.ShowDialog() == DialogResult.Cancel)
{
      frmn.Hide();
      this.Show();

}

Where n denotes the currently displayed form from 1 to n. I am using this code in every form and it allows me to retain the user input in the previous form. Is there any better solution?
Commented:
> I don't have DialogResult.Retry and DialogResult.Cancel in my application

That's fine! Was just a possible solution.  

> Is there any better solution?

Keeping your forms open and just hiding/showing them is a very appropriate idea!  There is nothing wrong with that approach at all.

Since it looks like you're thinking of a wizard style control, I had posted the following code on CodeProject that does something very similar to what you are trying. See if it helps at all.

http://www.codeproject.com/cs/miscctrl/wizardtab.asp

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial