Form1.cs, Form1.Designer.cs and Form1.resx

On Windows, the Visual Studio designer is a canvas: you drop a button on a form, drag it, and set its properties in a grid. What it saves is not a picture. It writes C# into Form1.Designer.cs, resources into Form1.resx, and leaves your own handlers in Form1.cs. This lesson reads all three as the code they are, because the designer does not exist in this container and the files are what a reader has to be able to follow.

Two files, one class

A form is one class split into two files with partial. The designer file holds InitializeComponent and one field per control; the form file holds the constructor that calls it and the event handlers. The split exists so that the designer can rewrite its half without touching yours.

Notes/ Notes.csproj Program.cs Form1.cs Form1.Designer.cs Form1.resx

The file names matter only to the designer and to a human reader: the SDK globs all *.cs files into the compilation, and .resx files become embedded resources by convention. Nothing in the project file names them.

The designer file

This is the shape the designer writes, with its own comments and its this. prefixes kept:

namespace Notes { partial class Form1 { private System.ComponentModel.IContainer components = null; protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code private void InitializeComponent() { this.label1 = new System.Windows.Forms.Label(); this.textBox1 = new System.Windows.Forms.TextBox(); this.button1 = new System.Windows.Forms.Button(); this.SuspendLayout(); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(16, 18); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(42, 15); this.label1.TabIndex = 0; this.label1.Text = "&Name:"; // // textBox1 // this.textBox1.Location = new System.Drawing.Point(90, 15); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(180, 23); this.textBox1.TabIndex = 1; // // button1 // this.button1.Location = new System.Drawing.Point(286, 13); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(94, 27); this.button1.TabIndex = 2; this.button1.Text = "&Say hello"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // Form1 // this.AcceptButton = this.button1; this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(400, 60); this.Controls.Add(this.button1); this.Controls.Add(this.textBox1); this.Controls.Add(this.label1); this.Name = "Form1"; this.Text = "Hello"; this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button1; } }

Four details are worth reading out of it. SuspendLayout and ResumeLayout bracket the setup so the form is not laid out after every single property. PerformLayout then applies AutoSize and the anchors in one pass. The Controls.Add calls are in reverse z-order: the button is added first, so it is at the back of the list, and the label, added last, is on top. AcceptButton makes the button the default one, so Enter presses it.

The handler is attached with new System.EventHandler(this.button1_Click). The long form is what the designer writes; this.button1.Click += this.button1_Click; is the same subscription and is what people write by hand. The method it names lives in the other half of the class, which is why the two files compile together and neither compiles alone.

The form file

using System; using System.Windows.Forms; namespace Notes { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show( $"Hello, {textBox1.Text}", "Greeting", MessageBoxButtons.OK, MessageBoxIcon.Information); } } }

This is the file you edit. It declares the base class once (: Form here), the constructor calls InitializeComponent, and every handler the designer wired up is a method in this file. The two blocks above are fragments: neither has a Main, so the checker counts them and does not build them.

The resource file

A .resx file is XML: a header that names the reader and the writer, then one data entry per resource. The designer uses it for localised strings, for the size of the form and for images referenced from a folder:

<?xml version="1.0" encoding="utf-8"?> <root> <resheader name="resmimetype"> <value>text/microsoft-resx</value> </resheader> <resheader name="version"> <value>2.0</value> </resheader> <resheader name="reader"> <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> <data name="$this.ClientSize" type="System.Drawing.Size, System.Drawing"> <value>400, 60</value> </data> <data name="label1.Text" xml:space="preserve"> <value>Name:</value> </data> </root>

Two kinds of entry live here. A value the designer localises (label1.Text) has a counterpart in Form1.ru.resx, and the runtime picks the file of the current culture. A property the designer stores as a resource ($this.ClientSize) is the size it could not keep in code without breaking the AutoScaleMode rules. Lesson 058, about localisation and accessibility, comes back to these files for translation.

The same form by hand

The same window in one file, with no designer and no partial, is the version this course builds. It is a complete program, and it is built, not run:

using System; using System.Drawing; using System.Windows.Forms; internal static class Program { [STAThread] private static void Main() { ApplicationConfiguration.Initialize(); Application.Run(new Form1()); } } internal sealed class Form1 : Form { private readonly Label _label = new() { AutoSize = true, Location = new Point(16, 18), Text = "&Name:", }; private readonly TextBox _name = new() { Location = new Point(90, 15), Size = new Size(180, 23), TabIndex = 1, }; private readonly Button _hello = new() { Location = new Point(286, 13), Size = new Size(94, 27), TabIndex = 2, Text = "&Say hello", }; public Form1() { Text = "Hello"; ClientSize = new Size(400, 60); AcceptButton = _hello; _hello.Click += OnHello; Controls.Add(_label); Controls.Add(_name); Controls.Add(_hello); } private void OnHello(object? sender, EventArgs e) { MessageBox.Show( $"Hello, {_name.Text}", "Greeting", MessageBoxButtons.OK, MessageBoxIcon.Information); } }

dotnet build app.csproj

Build succeeded.
0 Warning(s)
0 Error(s)

It is the same form: the same label, the same text box with the same TabIndex, the same default button, the same handler. What the designer buys is the canvas and the property grid, not a different runtime. What it costs is a second file that must stay in step with the first, which is why the course writes forms by hand and only reads the designer's output.

Exercises

  1. Add a third control to the hand-written form with the same properties the designer would write, and keep the tab order.
  2. Write the Form1.resx entry for a button whose text is &Save, with the ampersand escaped the way XML needs it.
  3. Explain why the designer writes this.Controls.Add(this.button1) before this.label1.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is split between Form1.cs and Form1.Designer.cs?

    Answer

    One partial class: the designer's setup and the handlers you write

    The two files are halves of the same class, joined by the partial keyword.

  2. Which method does the designer's file hold?

    Answer

    InitializeComponent

    It creates the controls and sets their properties; your handlers live in the other file.

  3. Why does InitializeComponent bracket its setup with SuspendLayout and ResumeLayout?

    Answer

    So the form is not laid out after every property change

    One layout pass after the setup is much cheaper than one per property.

  4. In which order are the controls added by the designer?

    Answer

    Reverse z-order: the last added is on top

    The first added control sits at the back of the Controls collection, which is the bottom of the z-order.

  5. What does AcceptButton = this.button1 do?

    Answer

    Makes Enter press that button

    The default button is pressed by Enter; CancelButton answers Escape.

  6. Which method name does the designer write for a new handler?

    Answer

    button1_Click, from the control's Name and the event

    The name is the control's Name, an underscore and the event, and it is only a convention.

  7. What kind of file is Form1.resx?

    Answer

    XML with a reader and writer header and data entries

    It is an XML resource file with resheader and data elements.

  8. What is Form1.ru.resx for?

    Answer

    The same resources in Russian, chosen by the current culture

    The runtime picks the resource file whose culture suffix matches the current culture.

  9. Why can neither of the two designer blocks be built alone?

    Answer

    Each is half a class and has no Main method

    A partial class without the other half and without an entry point is a fragment.

  10. What does the hand-written version use instead of a designer file?

    Answer

    A constructor that sets the same properties and calls Controls.Add

    The designer only writes property assignments and Controls.Add calls; a constructor does the same.

Every listing on this page was built with the .NET SDK 10.0.303 for net10.0-windows against the targeting pack Microsoft.WindowsDesktop.App.Ref 10.0.15 from the offline feed, in a Docker container on Alpine Linux 3.24.3 (x86_64). The programs were built, not run on Linux: WinForms needs Windows.

Article author: Arthur Isaev

Banner image