What WinForms is and where it fits

Windows Forms is the desktop user-interface stack that ships with .NET. A program builds a window out of Form objects and puts controls on it: buttons, text boxes, lists, grids. The classes live in System.Windows.Forms, the drawing in System.Drawing, and both are part of the Windows Desktop runtime, which exists for Windows only.

Every page of this course was built in a Linux container, because that is where the examples of this site run. A WinForms program compiles there, against the targeting pack of the Windows Desktop runtime, and it cannot run: the runtime is not installed, and there is no desktop to draw on. Every listing on this course is therefore built and not run, and every page says so. The behaviour of a control is described the way the documentation describes it, never as something observed here.

What Windows Forms is

A WinForms program is a loop around the Windows message pump. Application.Run(form) shows the form and then reads messages such as a mouse click or a key press, and sends each one to the control it belongs to. A control reacts by raising a .NET event, and the handler you wrote runs. That is the whole architecture: one thread that owns the window, a queue of messages, and ordinary .NET events on top.

The toolkit is old, and that is its selling point. It appeared in 2002 with the first version of the .NET Framework, it is still supported on .NET 10, and an application written in 2005 usually runs after a project-file change. It gives you the native Windows look without a styling engine, a visual designer in Visual Studio, and a control for almost everything a business application needs.

Where it fits

The three other stubs are courses that are being written; each of them names what it will cover. This course is the one for WinForms, and it assumes the language and the base class library of the C# course. When a lesson uses a language feature, it links the lesson that teaches it instead of explaining it again.

A first program

One file is enough for a window. The listing is one complete program: it declares the entry point, initialises the application and runs a form with a title. It is built for net10.0-windows from the project file of the next lesson, and it is not run: WinForms needs Windows.

using System; using System.Windows.Forms; internal static class Program { [STAThread] private static void Main() { ApplicationConfiguration.Initialize(); Application.Run(new Form { Text = "Hello, WinForms" }); } }

dotnet build app.csproj

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

The compiler needs the project file, the offline feed and the three using lines, and it needs [STAThread]: a WinForms program runs its user interface on a single-threaded apartment, and the attribute is how the runtime is told to start one. Without using System; that attribute is an unresolved name, which the lesson about the compiler shows as a real error. The real output of the build ends with a Time Elapsed line, which depends on the machine and is left out here.

What this course checks

Two kinds of code appear on the course. A program that draws a window is built and never started: the compiler is the check, and it catches the mistakes a UI programmer makes most often, from a missing using to a handler with the wrong signature. A program that computes something without a window, such as a validator, a view model or a presenter, is built and run in the container, and its output is printed on the page. That is the half of a WinForms application whose behaviour can be proven here, and the structure part of the course is built around it.

The designer is the one thing this course cannot show. On Windows, the Visual Studio designer writes a .Designer.cs file and a .resx file, and you move controls on a canvas. Here those files are read as code: the course shows what the designer writes, and how the same form is written by hand.

Exercises

  1. Add a second form to the first program and open it with ShowDialog. Which extra file does the project need for it, and what does Application.Run do with it?
  2. Change the form's title to an empty string and build. Does the compiler complain, and what does the documentation say the window shows then?
  3. Build the same program for net10.0 without the platform suffix and read the error.

Questions

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

  1. What does Windows Forms draw with?

    Answer

    The Windows desktop APIs, through the Windows Desktop runtime

    System.Windows.Forms builds on the Win32 controls and the Windows Desktop runtime, which exists for Windows only.

  2. Why can a WinForms program not run in this site's Linux container?

    Answer

    The Windows Desktop runtime is not installed there and there is no desktop

    The project compiles with EnableWindowsTargeting, but the runtime and the display are missing, so the program is built and not run.

  3. Which class starts the message loop of a WinForms application?

    Answer

    Application.Run

    Application.Run shows the form and reads the messages until the application closes.

  4. What does [STAThread] on Main declare?

    Answer

    That the user interface runs on a single-threaded apartment

    WinForms needs a single-threaded apartment, and the attribute is how the runtime is told to start one.

  5. Which of the four stacks in this lesson runs on the Linux server of this site?

    Answer

    ASP.NET Core

    A web application draws in a browser; the three desktop stacks need Windows or a device.

  6. Which stack is the successor of Xamarin.Forms and targets Windows, Android, iOS and macOS from one project?

    Answer

    MAUI

    MAUI is the cross-platform stack; a WinForms application is not portable to it.

  7. Which line of a real build output is a measurement of the machine and is left out on the pages of this course?

    Answer

    Time Elapsed

    The elapsed time is a measurement of the machine, not a fact of the program.

  8. What does the compiler need for the first program besides the source file?

    Answer

    A project file for net10.0-windows and the targeting pack of the desktop runtime

    The SDK project names the target framework, and the targeting pack supplies the reference assemblies.

  9. Which file does the Visual Studio designer write for a form?

    Answer

    A .Designer.cs file with InitializeComponent, next to a .resx file

    The designer writes the control setup into Form1.Designer.cs and the resources into Form1.resx.

  10. What is the architecture of a WinForms program in one sentence?

    Answer

    One thread that owns the window, a queue of messages, and .NET events on top

    Application.Run reads the message queue and dispatches each message to the control it belongs to.

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