Click here to Skip to main content
15,887,928 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello,

I have converted a C# project to CLI/C++ using a converter tool. Then tool has syntactically converted the code from C# to CLI/C++.

Tool converted & created .h & .cpp files for respective .cs files.

But, for window form & form.designer.cs file it has created .h & .cpp file.
The designer code is placed in .cpp file in InitializeComponent() function.

C# window form file converted code to .cpp is given below: -
File name - Form.cpp
#include "ToSeeGUIForm.Form.h"
using namespace System;
using namespace System::Collections::Generic;
using namespace System::ComponentModel;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Linq;
using namespace System::Text;
using namespace System::Windows::Forms;
namespace ToSeeGUIForm
{
    FormInCSharpProject::FormInCSharpProject()
    {
        InitializeComponent();
    }
    void FormInCSharpProject::DisposeObject(bool disposing)
    {
        if (disposing && (components != nullptr))
        {
            delete components;
        }
//C# TO C++ CONVERTER NOTE: There is no call to the base class's Dispose method in C++:
//      base.Dispose(disposing);
    }
    void FormInCSharpProject::InitializeComponent()
    {
        this->btnButton = gcnew System::Windows::Forms::Button();
        this->txtTextBox = gcnew System::Windows::Forms::TextBox();
        this->SuspendLayout();
        //
        // btnButton
        //
        this->btnButton->Location = System::Drawing::Point(110, 111);
        this->btnButton->Name = "btnButton";
        this->btnButton->Size = System::Drawing::Size(75, 23);
        this->btnButton->TabIndex = 0;
        this->btnButton->Text = "Button";
        this->btnButton->UseVisualStyleBackColor = true;
        //
        // txtTextBox
        //
        this->txtTextBox->Location = System::Drawing::Point(98, 71);
        this->txtTextBox->Name = "txtTextBox";
        this->txtTextBox->Size = System::Drawing::Size(100, 20);
        this->txtTextBox->TabIndex = 1;
        //
        // FormInCSharpProject
        //
        this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
        this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
        this->ClientSize = System::Drawing::Size(292, 266);
        this->Controls->Add(this->txtTextBox);
        this->Controls->Add(this->btnButton);
        this->Name = "FormInCSharpProject";
        this->Text = "FormInCSharpProject";
        this->ResumeLayout(false);
        this->PerformLayout();
    }
}


Due to this CLI/C++ project is not showing window form (view design) file for the .cpp file. This makes difficult to change gui. Changes are to be done through code only.

Is there option so I can change the .h & .cpp file to window form file?

Thanks
Kedar
Posted
Updated 11-Apr-11 18:29pm
v3
Comments
Kedar Kumbhar 5-Apr-11 8:08am    
If there is no way to change .h file to win form then, i think i will have to do it manually. I will create a new form.h and copy paste .h file code to it. I guess this will solve the issue.

And about CLI/C++, that's the requirement. Can't help...

1- C# designer uses partial classes: 1 class is splitted into several files. There is no equivalent in C++/CLI for the partial keyword, so you need to merge both files.

2- in C++/CLI you shouldn't use Dispose but Finalizers. See that link for more details:
http://msdn.microsoft.com/en-us/library/ms177197.aspx[^]

3- Do you really need to convert the project? Can't you just reference the C# assembly into your C++/CLI project? Dealing with WinForms in C++/CLI is really not recommanded...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 1-Apr-11 16:57pm    
My 5. (#3) it the only thing which is needed. Also, what's the problem with forms with C++/CLI? I think WinForms is generally evil, but that's due to ill Windows legacy (controls even keep showing flicker created many years ago in order to improve performance of slow CPUs; and that never been improved; Microsoft found more "clever" ways of slowing down computers :-)
--SA
Olivier Levrey 3-Apr-11 10:29am    
he he good point ;)
For converting a .h file to a form designer file, you will have to make changes in the .vproj file.
Example: - If we have a Login.h & Login.cpp files (The .cpp file should have InitializeComponent() function definition) and need to convert the Login.h file to Login.h[Design](For editing the Gui).

Search for Form.h file entry in the .vproj.
<File   RelativePath=".\MyProject\GUI\Form.h"    > </File>


Change the file type to "FileType = 3" and also add entry of its corresponding .resx file to the block.

<File
                    RelativePath=".\MyProject\GUI\Form.h"
                    FileType="3"
                    >
                    <File
                        RelativePath=".\MyProject\GUI\Form.resx"
                        SubType="Designer"
                        >
                    </File>
                </File>


Save the changes in .vproj and reload the project. This will convert simple.h file to a designer file.


Regards
Kedar
 
Share this answer
 
This is what I had to do in the *.vcxproj (C++/cli)

<ItemGroup>
<ClInclude Include="Form1.h">
<FileType>CppControl</FileType> [add this line]
</ClInclude>
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Form1.resx">
<DependentUpon>Form1.h</DependentUpon> [add this line]
<SubType>Designer</SubType>
</EmbeddedResource>
</ItemGroup>

save and restart the project :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900