locked
Create C++ Windows Forms application in Visual Studio 2017 RRS feed

  • General discussion

  • Background:

    From Windows Forms Application in Visual C++ ???!!!!:

    "MS removed the WinForms C++/CLI template starting in either VS2012 or VS2013. The official explanation is that they want to encourage managed desktop UI development in C# or VB and relegate C++/CLI to serve as glue between native back end code and managed UI code."

    But for some reason, there are still many people who have reason to create a Windows Forms project of type C++/CLI, for these people, the following steps will show you how to create a C++/CLI Windows Forms app in Visual Studio 2017.

    Environmental configuration:

    Modify the Visual Studio 2017 installer and make sure the Visual Studio 2017 have installed C++ package with C++/CLR:

    Create new project:

    1, If you have installed all the package and components, then open Visual Studio 2017 and create a new project:

    2, You need to configure some properties first, right click on the Project and choose Properties option:

    Add new item:

    1, Right click on the project to add a new item, select UI under Visual C++,then choose Windows Form:

    In general, you will get error as below in this step:

    2, Don't worry, close it and add the following code to the MyForm.cpp:

        using namespace System;
        using namespace System::Windows::Forms;
    
        [STAThreadAttribute]
        void Main(array<String^>^ args) {
    	Application::EnableVisualStyles();
    	Application::SetCompatibleTextRenderingDefault(false);
            //WinformCDemo is your project name
    	WinformCDemo::MyForm form;
    	Application::Run(%form);
        }

    3, After that, save it and close the Visual Studio, then reopen your Project. Note that when you reopen your Project, remember to build(or run directly) it first:

    4, Now you can design your app in the form by double click MyForm.h:

    That's all.

    Best Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, December 6, 2018 7:42 AM

All replies

  • The current version of Visual Studio 2017:

    Microsoft Visual Studio Community 2017
    Version 15.9.4
    VisualStudio.15.Release/15.9.4+28307.222
    Microsoft .NET Framework
    Version 4.7.03056

    Does not allow uppercase for main.  Substitute main for Main in the linker->advanced setting and in the MyForm.cpp.

    Monday, February 18, 2019 5:50 PM
  • I love you Dude, i swear i have about five hours looking in all the forums, i have twenty tab's looking for that, and always appeared the same Link Error, until i read your comment . I can't believe that only an uppercase could cause so much trouble, i really thank you for existing and comment here ChicagoCGuy ♥♥♥

    • Edited by CamiloBY Tuesday, April 2, 2019 2:54 AM
    Tuesday, April 2, 2019 2:53 AM
  • With Visual Studio 15.7.3 I not only had to remove the capitalization for "Main" but he also had to change from "void main" to "int main" to resolve all linker errors.
    Tuesday, May 21, 2019 8:29 PM
  • If you cant get the namespace to work for the second to last line in the main() function it might be because for some reason namespaces take out special characters such as "_" and "+" as seen in this tutorial. If that's your problem, and you don't know what is omitted; take a look at the solution explorer, expand the header files folder, and then you should see another thing you can expand that has this by it "{ }". That is your namespace that you need to use. Hope that could help someone else that struggled as bad as me :) 

    Also don't uppercase main as others have said
    Wednesday, July 3, 2019 4:44 PM
  • Alright, Thank you so much! It took me hours to get it to work!
    Sunday, April 5, 2020 10:07 PM
  • Hello!

    Sir there is no UI option in my Visual Studio 2019 under new item menu.

    Help.

    Wednesday, July 8, 2020 9:32 AM
  • I just tested with VS 2019 16.7. Make sure you have installed the C++/CLI optional component and the .Net desktop development. When I only had the .Net development installed I didn't see the wizard option for a Windows Form project so the C++/CLI components is very much required.
    Thursday, August 6, 2020 8:31 PM
  • please use this simple steps:

    1-Download C++ Express 2010 from VS download page

    2-Install it

    3-If you are using VS greater than 2010 then all you need is create a project with c++ 2010

    4-Copy it to the VS, let's say, VS 2017/projects folder.

    5-Use VS 2017m open the project and there it is, it will work exactly as you create with VS 2017

    6-You need not go thru modification and stuffs

    Happy coding

    Friday, August 14, 2020 1:19 PM
  • using namespace System;
    using namespace System::Windows::Forms;

    [STAThreadAttribute]

    int APIENTRY wWinMain(_In_ HINSTANCE hInstance, 
    _In_opt_ HINSTANCE hPrevInstance,
    _In_ LPWSTR    lpCmdLine,
    _In_ int       nCmdShow)


    /*int main(array<String^>^ args) - this one runs with console, the upper one with  Windows (/SUBSYSTEM:WINDOWS) in linker */

    {
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);
    //WinformCDemo is your project name
    WinformCDemo::MyForm form;
    Application::Run(%form);

    }

    Microsoft Visual Studio Enterprise 2019

    Monday, August 17, 2020 9:00 PM