Search code examples
winformsc++-cliwindows-forms-designer

Splitting C++/CLI code between .h and .cpp file


Is it possible to split a form class code between .h and .cpp file in C++/CLI like we do with native C++

When I do that I get parse error in the designer view.


Solution

  • Yes you can.

    Let the method definition in header, for example the constructor and destructor:

    Form1(void);
    ~Form1();
    

    and create a .cpp file, or just edit an existing : include "formName.h" (dont forgot namespace), next:

    Form1::Form1(void)
    {
        // ...
    }
    
    Form1::~Form1()
    {
       // ...
    }
    

    For events (Click, Load, etc) load the event, keep definition of method in header and put the implementation in source file.

    .h:

    System::Void Button_Click(System::Object ^sender, System::EventArgs ^e);
    

    .cpp:

    Void Button_Click(Object ^sender, EventArgs ^e)
    {
        MessageBox::Show("Hello, world !");
    }