(Why / How) should you use .designer files?

I never liked the Visual Studio tab [Design]

when creating my forms, so I always create my forms programmatically from scratch and I only use one file .cs

, for example Form1.cs

. Just today I noticed that creating a new tabbed form [Design]

also creates a file named *.Designer.cs

that handles all the design related stuff.

Since I am making my forms by hand, should I use a file *.Designer.cs

? If so, when, how and why should I use it? What does a file *.Designer.cs

meant to be executed just to separate the generated code VS code from user code, or have a deeper meaning?


Here's an example of how I create my forms:

class MyForm : Form
{
    TextBox file;
    Button open, close;

    MyForm()
    {
        InitControls();
    }

    void InitControls()
    {
        file = new TextBox();
        file.Location = Point(...);
        open = new TextBox();
        open.Text = "Open File";
        ...
    }
}

      

Should I separate my method InitControls

and variable declarations before the .Designer.cs

file?

+3


source to share


3 answers


I can't remember the exact dates, but I know that until a certain point Visual Studio did not use a separate .designer class and contained all the designer and logic in the same class as the region. The fact that it is no longer the case should tell you that it is widely considered inferior to splitting code files.

Since then, the Visual Studio developer has been using a modifier partial

to separate UI logic from UI design. When you call InitializeComponent

, you basically call all the code generated by the constructor to create the UI.



I'm pretty sure the designer is creating two separate files to abstract the generated code from the developer, as it doesn't have to be manually modified. Since you will not be using the constructor, it will be of little use to you that you should consider.

I think I don't want to use awesome design tools that help make the .NET RAD environment counter-intuitive and won't fly well with many other coders, so I would say just do what you like. If you find that surrounding your design code in a region in a single class is cleaner and cleaner than using a separate designer file, then so be it.

+4


source


Visual Studio 2005 (C # 2) is introduced .Desinger.cs

to separate the code generated by the Windows Forms designer from the code that the programmer writes. The file's only use is to hold a InitializeComponent

developer-generated method . However, it is not recommended to use your own UI code in this file because the WinForms developer parses the file content (essentially just a method InitializeComponent

) to create the form in the designer. It doesn't make sense to paste your own code into the file .Designer.cs

.



By the way, WinForms technology is designed in such a way that you have to use a constructor or suffer in pain!

+3


source


*.Designer.cs

- it is nothing more than Code File

that use force partial class

to save Application Code

and UI Design

in two separate files.

Since you are doing the design by Form

hand, there is no need for a constructor file.

*.Designer.cs

contains a declaration in all Controls

and a method private void InitializeComponent()

that initializes the controls!

+1


source







All Articles