Call a form and return to that form. C++ Winforms

José Carlos 886 Reputation points
2023-01-17T13:05:44.2366667+00:00

I have 2 forms. form1 calls fmr2 normally. Before calling I give a Hide in form1. After finishing the tasks in form2, I want to close it and reopen form1. I'm not getting. If I put the include "frmPrincipal.h" in form2, it gives an error when calling form1.

Here's the code:

			Chama o form2

frmJogo^ Form2 = gcnew frmJogo();
Form2->ShowDialog();
this->Hide();

     Chamando o form1 de volta
frmPrincipal^ newform1 = gcnew frmPrincipal();
newform1->ShowDialog();
this->Hide();
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,833 questions
{count} votes

Accepted answer
  1. YujianYao-MSFT 4,291 Reputation points Microsoft Vendor
    2023-01-18T01:42:25.6266667+00:00

    Hi @José Carlos Souza,

    Calling Form1's header file in Form2 is a bad practice and you should avoid it. Assume the following code is implemented with a button.

    frmPrincipal^ newform1 = gcnew frmPrincipal();
    newform1->ShowDialog();
    this->Hide();
    

    The correct way is to add a declaration of buttonclick event in Form2.h, and then implement it in Form2.cpp. Form2.h

     //Generate a click event after double-clicking the button  
    //modify it to a function declaration  
     private:  
    
        System::Void button1_Click(System::Object ^ sender,  
                                   System::EventArgs ^ e); 
    
    

    Form2.cpp

    #include "Form1.h"  
    #include "Form2.h"  
    using namespace System;  
    using namespace System::ComponentModel;  
    using namespace System::Collections;  
    using namespace System::Windows::Forms;  
    using namespace System::Data;  
    using namespace System::Drawing;  
     System::Void Project50::Form2::button1_Click(System::Object ^ sender,  
    
                                    System::EventArgs ^ e) {  
        Project50::Form1^ dlg = gcnew Project50::Form1();
        dlg->Show();
        this->Hide();
     }  
    

    Best regards,

    Elya


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. José Carlos 886 Reputation points
    2023-01-18T19:46:29.1266667+00:00

    Hi

    Perfect. The solution worked. I would never get that. I'm learning C++ and I didn't know that .cpp could have code.

    Many thanks again for the help.

    Taveira

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.