modern-forms / Modern.Forms-Templates

dotnet templates for Modern.Forms
MIT License
1 stars 0 forks source link

template designer file, w/ future designer in mind #2

Open ghost opened 2 years ago

ghost commented 2 years ago

In concert with https://github.com/modern-forms/Modern.Forms/issues/18, it might help to start thinking of a canonical arrangement for the form-definition code in the designer partial-file (MainForm.Designer.cs). The one used by the actual WinForms designer seemed to work well for them, so I used that arrangement as a model when starting some new projects on Linux (Eto.Forms+.Net6, WinForms+Mono) where there was not yet a designer, but might be some day. With that in mind, after I opened the project created from Modern.Forms-Templates (dotnet new modernforms), I re-worked the generated designer-file code to match what the designer might output. The result looks like...

namespace ModernFormsApp1
{
    partial class MainForm
    {

        #region Windows Form Designer generated code

        private void InitializeComponent()
        {
            this.button = new Modern.Forms.Button();
            this.label = new Modern.Forms.Label();
            //
            // button
            //
            this.button.Location = new System.Drawing.Point(10, 40);
            this.button.Name = "button";
            this.button.Size = new System.Drawing.Size(75, 23);
            this.button.TabIndex = 0;
            this.button.Text = "Click Me!";
            this.button.Click += new System.EventHandler<Modern.Forms.MouseEventArgs>(this.ButtonClicked);
            //
            // label
            //
            this.label.Location = new System.Drawing.Point(10, 80);
            this.label.Name = "label";
            this.label.Size = new System.Drawing.Size(250, 13);
            this.label.TabIndex = 1;
            // 
            // MainForm
            // 
            this.Controls.Add(this.button);
            this.Controls.Add(this.label);
            this.Text = "ModernFormsApp1";
        }

        #endregion

        private Modern.Forms.Button button;
        private Modern.Forms.Label label;

    }
}

Note that the partial class does not need to repeat the public keyword or re-declare the base in order to compile. Note that the partial class does not need the using statements either. Note that the InitializeComponent method can be private. The arrangement seemed to be that the declaration of controls is at the bottom, outside the InitializeComponent call, the instantiation is done at the top of InitializeComponent, and then the setting of individual properties, controls configured before the form and then added to it, and everything fully/explicitly qualified. Just some food for thought.

ghost commented 2 years ago

BTW, I do not know why the declarations were outside of the generated-code region, but I preserved/emulated that arrangement in my projects until I find an explanation.

ghost commented 2 years ago

Q. Why is everything fully qulified? A. This must have been to allow for unambiguous identification of objects in the designer, even if 3rd party controls become involved.

Q. Why are the declarations and instantiation and the setting of properties all separated, when there are more compact statements possible? A. This form is as atomic as can be, simplifying the parsing of the code to find the objects, their properties and values, and the event-handlers being assigned to their events.

ghost commented 2 years ago

For more elaborate examples of this arrangement, see

1) https://github.com/sjsepan2/EtoFormsApp2/blob/master/EtoFormsApp2/MainForm.eto.cs in https://github.com/sjsepan2/EtoFormsApp2

2) https://github.com/sjsepan2/MonoApp1/blob/master/MainWindow.Designer.cs in https://github.com/sjsepan2/MonoApp1

jpobst commented 2 years ago

Thanks, I took a few of your suggestions in https://github.com/modern-forms/Modern.Forms-Templates/commit/0d334a5695ee577efb4fa8144e932ce2efbe7e4f.

I think it's probably a bit premature to try to figure out how a hypothetical designer would serialize the code, so we'll leave that until a designer exists.

ghost commented 2 years ago

Thanks, I took a few of your suggestions in 0d334a5.

I think it's probably a bit premature to try to figure out how a hypothetical designer would serialize the code, so we'll leave that until a designer exists.

Cool, Thanks. Agreed -- just some notes for possible future reference.