Add a Form to a Project

srfpala 111 Reputation points
2020-12-20T00:51:44.893+00:00

Using VS2019 VC++ under Win10 building CppCLR_WinformsProject.
Project Loads and Builds as expected.
I add a button to form1 and test it.... OK
Now when I click the button I'd like to display another form - Form2.
I think the new form should be a new item Thus Add new item seems logical ?
But Form is not shown. I see Code, Formatting , Data and others.
What should I do ?
TIA
Bob

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. WayneAKing 4,921 Reputation points
    2020-12-20T02:05:00.397+00:00

    Where are you selecting that option? Describe the steps - menus used, etc.

    I'm not using VS2019, but in VS2017 for a C++ WinForms app I would
    usually go to the menus:

    Project->Add New Item->UI->Windows Form

    • Wayne
    0 comments No comments

  2. srfpala 111 Reputation points
    2020-12-21T00:41:12.807+00:00

    With Form1.h [Design] visible, I also try
    Project | Add New Item and see
    Code,Formatting,Data,Resource,Web,Utility,
    Property Sheets,Test, HLSL Graphics ,Online
    Note that there is no UI->Windows Form
    Bob

    0 comments No comments

  3. WayneAKing 4,921 Reputation points
    2020-12-21T01:06:12.74+00:00

    In the Properties for the project make sure that you have

    Common Language Runtime Support (/clr)

    selected under

    General->Common Language Runtime Support

    • Wayne
    0 comments No comments

  4. Kyle Wang 5,531 Reputation points Microsoft Vendor
    2020-12-21T03:31:14.987+00:00

    To add a new form in CppCLR_WinformsProjekt1 VS2019, here is a workaround maybe you can refer to.

    First, right click "Headerdateien" and select "Add/New item...".

    49816-1.png

    Click tag "UI", choose "Windows Form" and add it.

    49865-2.png

    Now maybe you will get an error as below.

    49871-3.png

    Just close it and add the following code to the MyForm1.cpp:

    using namespace System;  
    using namespace System::Windows::Forms;  
      
    [STAThreadAttribute]  
    void Main(array<String^>^ args) {  
        Application::EnableVisualStyles();  
        Application::SetCompatibleTextRenderingDefault(false);  
        CppCLR_WinformsProjekt1::MyForm1 form;  
        Application::Run(% form);  
    }  
    

    Then save it and close the Visual Studio, then reopen the Project and build/run it.

    You will see the "MyForm1.h" in "Solution Explorer".

    49765-4.png

    0 comments No comments

  5. srfpala 111 Reputation points
    2020-12-22T01:31:33.613+00:00

    Thank you KyleWang
    I tried your suggestion but errors resulted in these two lines of code:
    CppCLR_WinformsProjekt1::MyForm1 form;
    Application::Run(% form);
    // -------------------------------Errors follow----------
    name followed by :: must be a class ....
    expected a ';'
    identifier 'form' is undefined
    // -----------------------------------


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.