Visual Studio creates designer files (such as frmMain.Designer.cs) for WinForms forms. In these files, there is a method called "InitializeComponent", and it has a comment: do not modify the contents of this method with the code editor.
Now I want to know: Can these files be edited outside of Visual Studio? Is there any mystery in these files?
I am trying to port WinForms projects to Linux, using Mono. To maintain these projects, I want to use a WinForms designer. Visual Studio does not run on Linux, so I can only use some other tool. For example, the web page "https://www.mono-project.com/archived/winforms_designer/" talks about an unfinished designer. Maybe this designer can be finished with some efforts. Obviously, such a tool needs to edit these designer files. So, I want to know: Can these files be edited outside of Visual Studio? Is there any mystery in these files?
2
Answers
You can edit these files; there’s no mystery to them. In fact you can probably even do it inside of Visual Studio.
The reason you shouldn’t edit these files is that they are auto-generated by your IDE, so when you edit them, you risk:
Sometimes, editing the designer file can be helpful. For example if you have a lot of controls to add, you can add them more efficiently by copying and pasting code and editing some positioning values rather than using the designer, which can be slower.
In this case, you wouldn’t lose your changes because the changes will be reflected in the IDE designer window. Anything you see in that window will be written back to the designer file whenever the IDE saves that file.
It is possible to edit designer files created by Visual Studio for WinForms forms outside of Visual Studio, such as frmMain.Designer. cs. The auto-generated code for initializing and setting the form’s controls is included in these files, which are standard C# code files.
These designer files’ "InitializeComponent" method is in charge of configuring the form’s controls and their settings and event handlers. The warning from Visual Studio that you referred to, "Do not modify the contents of this method with the code editor," is intended to discourage manual modification of this method because the designer may overwrite any changes made inside the process.
Although the designer files themselves can be modified, using the Visual Studio designer is typically advised for making changes to WinForms forms. It is simpler to visually design and edit the form layout, controls, and properties with the designer’s graphical interface.
The Mono Project does offer a WinForms designer, as you said, for the purpose of converting WinForms programs to Linux using Mono. The designer is still regarded as incomplete, but with some work, it might be feasible to complete and improve it such that it satisfies your needs. You’ll probably need to alter the designer files produced by Visual Studio outside of Visual Studio if you choose to work on improving the Mono WinForms designer.
In conclusion, even though the designer files can be changed outside of Visual Studio, using the Visual Studio designer for WinForms applications is advised whenever practical. You might need to adjust these files if you decide to use the Mono WinForms designer, but be careful not to change the automatically produced code inside the "InitializeComponent" method to avoid conflicts with the designer’s functionality.