0

I'm developing a C# (compact framework) app which and I need to have 3 forms. I can go from form1 (main) to form2, and from form1 to form3. I also want to be able to switch from form2 to form3. Form3 must be always the same (I need to create it when app starts and close it when app ends).

On form1, on "Go to form 2" button

form2.Show(); form2.BringToFront();

On form1, on "Go to form 3" button

form3.Show(); form3.BringToFront();

On form2, on "Back to form 1"

this.Hide();

On form3, on "Back to form 1"

this.Hide();

But how to switch from form2 to form3 ?

Thank you for any help!

Filipe Pinto
  • 311
  • 3
  • 17

3 Answers3

0

I think there's a multi part solution here.

  1. First, if your form3 is a "global" form and should not be re-instantiated, then you should store that off as a static internal variable somewhere for easy reference.
  2. When form1 opens form2, you should hook into the FormClosed event.
  3. In form2, when the user clicks the button (or whatever) close the form2.
  4. In your event handler, for FormClosed, reshow the global form3.
mphilipp17
  • 31
  • 3
0

Thanks for the all the contributions! I ended up building a flexible solution that can be extended to many forms.

When the app starts I create all the required forms. I have an auxiliary class with a (global) variable that holds the current active form (I've used an int but it could be a string with the form name).

static class GlobalClass
{
    private static int m_currentActiveForm = 1;

    public static int currentActiveForm
    {
        get { return m_currentActiveForm; }
        set { m_currentActiveForm = value; }
    }
}

On form1 I have included a timer that checks every 100ms the var currentActiveForm. When a change is detected the corresponding form is displayed.

private void timer2_Tick(object sender, EventArgs e)
    {

        if (GlobalClass.currentActiveForm != lastActiveForm)
        {
            switch (GlobalClass.currentActiveForm)
            {
                case 1:
                    form2.Hide();
                    form3.Hide();
                    this.BringToFront();
                    break;
                case 2:
                    form2.Show();
                    form2.BringToFront();
                    break;
                case 3:
                    form3.Show();
                    form3.BringToFront();
                    break;
             }
       }
}

Then, on any form, to switch to a new form, I just need to assign a new value to GlobalClass.currentActiveForm which is visible in all forms.

Filipe Pinto
  • 311
  • 3
  • 17
  • I'm sorry, but that answer makes my eyes bleed. I don't see any reason for a timer. Create a FormManager class that creates and owns all the forms. Give it methods called SwitchToOne, SwitchToTwo, etc and put your form show and hide code there. – tcarvin Jan 08 '16 at 13:39
  • Does Compact Framework support using a class inheriting from [ApplicationContext](https://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext(v=vs.110).aspx)? One [example](https://stackoverflow.com/a/13407161/111794) and another [example](https://stackoverflow.com/a/13407161/111794). – Zev Spitz Jan 23 '18 at 23:58
-1

On form2, click "Go to form 3" button

form3.Show();

On form3, click "Back to form 2" button

this.Hide();
Suvro
  • 352
  • 2
  • 13