locked
VC++ switch between windows forms RRS feed

  • Question

  • Hello. I've got a problem that I haven't been able to solve after a long time searching.

    It's a simple problem. I have two forms.
    1) Form1
    2) Form2

    I need to get from Form1 to Form2 and vice versa.

    I can get from Form1 to Form2 easily, but I can't get back from Form2 to Form1.

    I have the #include "Form2.h" on Form1.h and the following code in the click event of button1.

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    Form1::Visible=false;
    Form2 ^form2 = gcnew Form2();
    form2->ShowDialog();
    }

    Does anyone know how to solve this? Thanks.
    Thursday, November 4, 2010 2:48 PM

Answers

  • Well, Form1 is my "Main Menu". So I have buttons which lead to other forms. So once I click a button on my "Main Menu", it opens up a new form and hides the Main Menu.
     
    What I want to do now, is to have a button on the new form (form2) which goes back to "Main Menu".
    If Form2 is a modal form then you can hide Form1 before calling ShowDialog(), and show it again after ShowDialog() returns. Form2 does not need to know anything about Form1.
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by shaks2 Friday, November 5, 2010 4:11 PM
    Friday, November 5, 2010 3:04 PM

All replies

  • Hello. I've got a problem that I haven't been able to solve after a long time searching.
     
    It's a simple problem. I have two forms.
    1) Form1
    2) Form2
     
    I need to get from Form1 to Form2 and vice versa.
     
    I can get from Form1 to Form2 easily, but I can't get back from Form2 to Form1.
     
    I have the #include "Form2.h" on Form1.h and the following code in the click event of button1.
     
    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    Form1::Visible=false;
    Form2 ^form2 = gcnew Form2();
    form2->ShowDialog();
    }
     
    Does anyone know how to solve this? Thanks.
    Why do you need to "get back from Form2 to Form1"?
     
    When ShowDialog returns, you will be back in your button handler. That is how modal forms work.
     
    You can retrieve any required values from Form2, and update Form1 as needed.
     

    David Wilkinson | Visual C++ MVP
    Thursday, November 4, 2010 11:04 PM
  • Well, Form1 is my "Main Menu". So I have buttons which lead to other forms. So once I click a button on my "Main Menu", it opens up a new form and hides the Main Menu.

    What I want to do now, is to have a button on the new form (form2) which goes back to "Main Menu".

    Friday, November 5, 2010 2:30 PM
  • Well, Form1 is my "Main Menu". So I have buttons which lead to other forms. So once I click a button on my "Main Menu", it opens up a new form and hides the Main Menu.
     
    What I want to do now, is to have a button on the new form (form2) which goes back to "Main Menu".
    If Form2 is a modal form then you can hide Form1 before calling ShowDialog(), and show it again after ShowDialog() returns. Form2 does not need to know anything about Form1.
     

    David Wilkinson | Visual C++ MVP
    • Marked as answer by shaks2 Friday, November 5, 2010 4:11 PM
    Friday, November 5, 2010 3:04 PM
  • Yes, it worked. Thanks. So simple lol.

     

     

     

    Friday, November 5, 2010 4:12 PM
  • I think, You must not Form1::Visible = false; ^^;
    Friday, March 25, 2011 2:12 PM