Windows Forms (.NET Framework)

Windows Forms (often abbreviated as WinForms) is a free, open-source, cross-platform user interface (UI) framework for creating desktop applications for Windows, macOS, Linux, and mobile platforms. It is a modern rewrite of the original Windows Forms technology, designed to be compatible with .NET Core and .NET 5+.

Note: This documentation focuses on Windows Forms within the context of the .NET Framework. For information on modern Windows Forms development with .NET Core and later versions, please refer to the .NET documentation.

Introduction to Windows Forms

Windows Forms provides a rich set of controls, from basic elements like buttons and text boxes to more complex components like data grids and tree views. It allows developers to create visually appealing and interactive user interfaces with minimal code.

Key features include:

Getting Started with Windows Forms

Creating Your First Windows Forms Application

To start building a Windows Forms application, you typically use Visual Studio. Here's a simplified overview:

  1. Open Visual Studio and create a new project.
  2. Select the "Windows Forms App (.NET Framework)" template.
  3. Design your user interface by dragging and dropping controls from the Toolbox onto the Form.
  4. Write code behind the controls to handle user interactions and application logic.

Key Concepts

The Form Object

The `Form` class is the foundation of any Windows Forms application. It represents the main window of your application and serves as a container for other controls.

using System.Windows.Forms; public class MyForm : Form { public MyForm() { this.Text = "Hello, Windows Forms!"; this.Size = new System.Drawing.Size(300, 200); } }

Controls

Controls are the building blocks of your UI. They include:

Events

Windows Forms uses an event-driven model. Controls raise events when something significant happens (e.g., a button is clicked, text changes in a textbox). You can subscribe to these events and write event handler methods to respond.

public class MyForm : Form { private Button _myButton; public MyForm() { this.Text = "Event Handling Example"; _myButton = new Button(); _myButton.Text = "Click Me"; _myButton.Location = new System.Drawing.Point(50, 50); // Subscribe to the Click event _myButton.Click += new EventHandler(MyButton_Click); this.Controls.Add(_myButton); } // Event handler method private void MyButton_Click(object sender, EventArgs e) { MessageBox.Show("Button was clicked!"); } }

UI Design and Layout

Anchoring and Docking

Windows Forms provides properties like `Anchor` and `Dock` to control how controls resize and reposition themselves when the parent container is resized. This is crucial for creating responsive UIs.

Layout Panels

For more complex layouts, consider using layout panels:

Data Binding

Windows Forms offers powerful data binding capabilities, allowing you to connect UI controls directly to data sources such as databases, collections, or business objects. This significantly simplifies displaying and updating data.

Graphics and Custom Drawing

You can perform custom drawing on forms and controls using the `Graphics` object provided in the `Paint` event handler.

Further Reading