Form calls Form

srfpala 111 Reputation points
2020-12-27T22:56:18.7+00:00

TwoFormsTest3 Project
I have a Windows CppCLR_Winforms App built with VS2019 running in Win10.
frmMain , the opening form has a button whose click event attempts to open
MyForm1. frmMain has an #include "MyForm1.h" statement.
C++/CLI support for v142 build tools has been installed.
In frmMain button click event I try the following unsuccessful code attempts

// ========= I think fmrMain needs some code to access MyForm1
Statements I've tried
MyForm1^ MyForm1 = gcnew MyForm1(); // Says E0020 MyForm1 is undefined
MyForm1 form= new MyForm1(); // Fails also
MyForm1.TopLevel = false;// Fails also
MyForm1.Show(); // Fails also
=============================================== /

So how do I make MyForm1 visible to frmMain ?

TIA
Bob

Developer technologies | Windows Forms
Developer technologies | C++
{count} votes

Accepted answer
  1. Daniel Zhang-MSFT 9,651 Reputation points
    2020-12-28T05:47:29.56+00:00

    Hi srfpala,
    First, create C++ winform project by the an earwig's answer.
    Then, add a new form(MyForm1).
    Right click the project name->Add->New Item->UI->Windows Form->Add.
    Finally, use the following code to call MyForm1.

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {  
    	MyForm1^ f1 = gcnew MyForm1();  
    	f1->ShowDialog();  
    }  
    

    Test result:
    51526-1228.gif
    Best Regards,
    Daniel Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    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.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. José Carlos 886 Reputation points
    2023-01-17T19:19:45.09+00:00

    HI,

    I have a program that when clicking the button on form1, it closes this form1 and opens form2. Now how do I put a button on form2 to close that form2 and reopen form1. I'm not able to do this. I've tried everything.

    Thanks

    0 comments No comments

  2. José Carlos 886 Reputation points
    2023-01-17T22:04:21.35+00:00

    Daniel's answer above doesn't work for me. Calling a form is fine. I want to close the open form and open another one that is hidden.

    Tks

    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.