(Why/How) should I use .designer files?
Asked Answered
A

3

9

I've never liked the Visual Studio [Design] tab when creating my forms, which is why I always create my forms programmatically from a scratch and I only use one .cs file, such as Form1.cs. Just today I noticed that creating a new Form with the [Design] tab also creates a file called *.Designer.cs, which handles all the design related stuff.

Since I'm doing my forms manually, should I still use *.Designer.cs file? If so, when, how and why should I use it? What is the *.Designer.cs file meant to do, is it only to separate VS auto-generated code from the user code, or does it have a deeper meaning?


Here's an example on 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 InitControls method and variable declarations to .Designer.cs file?

Assuage answered 16/2, 2013 at 15:17 Comment(7)
It's obvious mahi, just call InitializeComponent() which is defined in Designer.cs :))Zarzuela
@Zarzuela As I mentioned, I'm not using the VS auto-generated code :PAssuage
It's up to you, just want to say you hello mahi, glad to see you here with new account :)Zarzuela
sure you know, your mahi account were deleted after our long conversation :)Zarzuela
@Zarzuela Uhh I'm sorry, but I think you're confusing me with someone else.Assuage
let us continue this discussion in chatZarzuela
How does this generated code do during merges of feature branch to main codeline? Are there merge tools available that make this easy?Peeler
A
6

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

Since then the Visual Studio designer makes use of the partial modifier to separate the UI logic and UI design. When you call InitializeComponent you basically invoke all of the code, generated by the designer to create the UI.

I am sure the designer creates two separate files to abstract the generated code from the developer as it shouldn't be amended manually. As you will not be using the designer then this serves little advantage to you, which is something to consider.

I think choosing not to use the awesome designer tools that assist in making .NET a RAD environment is illogical and would not fly well with many other coders so I would say - just do what you like. If you find surrounding your designer code in a region in a single class is cleaner and more legible than using a separate designer file then, so be it.

Anthracite answered 16/2, 2013 at 15:43 Comment(1)
Thanks for the explanation! :) I guess I won't be using the .Designer.cs then. I find the auto-generated code guite disturbing, it lacks many features I need in my programs (or maybe I can't use the designer properly) and the code is somewhat difficult to read. Also I prefer having full control over what I'm doing, and not letting the code to be auto-generated.Assuage
P
3

The .Desinger.cs is introduced in Visual Studio 2005 (C# 2) to separate the code that Windows Forms designer generates from the code that the programmer writes. The only use of the file is to hold the InitializeComponent method that the designer generates. However, putting your own UI code in that file is not recommended, becuase the WinForms designer parses the contents of the file (acutally only the InitializeComponent method) to create the form in the designer. There is no point in putting your own code in the .Designer.cs file.

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

Pharmacognosy answered 16/2, 2013 at 15:39 Comment(0)
S
2

*.Designer.cs is nothing but a Code File that harness the power of partial class just to keep the Application Code & UI Design in two separate files.

Since you are doing Form design manually, there is no need for the designer file.

*.Designer.cs contains declaration on all the Controls and the Method private void InitializeComponent(), that initializes the Controls!

Sugarplum answered 16/2, 2013 at 15:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.