1---2name: winforms3description: Build, maintain, or modernize Windows Forms applications with practical guidance on designer-driven UI, event handling, data binding, MVP separation, and migration to modern .NET. USE FOR: working on Windows Forms UI, event-driven workflows, or classic LOB applications; migrating WinForms from .NET Framework to modern .NET; cleaning up oversized form code. DO NOT USE FOR: unrelated stacks; generic tasks that do not need this specific guidance. INVOKES: inspect the repository context, edit targeted files, and run relevant build, test, lint, or validation commands when changes are made.4---56# Windows Forms78## Trigger On910- working on Windows Forms UI, event-driven workflows, or classic LOB applications11- migrating WinForms from .NET Framework to modern .NET12- cleaning up oversized form code or designer coupling13- implementing data binding, validation, or control customization1415## Workflow16171. **Respect designer boundaries** — never edit `.Designer.cs` directly; changes are lost on regeneration.182. **Separate business logic from forms** — use MVP (Model-View-Presenter) pattern. Forms orchestrate UI; presenters contain logic; services handle data access.19 ```csharp20 // View interface — forms implement this21 public interface ICustomerView22 {23 string CustomerName { get; set; }24 event EventHandler SaveRequested;25 void ShowError(string message);26 }2728 // Presenter — testable without UI29 public class CustomerPresenter30 {31 private readonly ICustomerView _view;32 private readonly ICustomerService _service;33 public CustomerPresenter(ICustomerView view, ICustomerService service)34 {35 _view = view;36 _service = service;37 _view.SaveRequested += async (s, e) =>38 {39 try { await _service.SaveAsync(_view.CustomerName); }40 catch (Exception ex) { _view.ShowError(ex.Message); }41 };42 }43 }44 ```453. **Use DI from Program.cs** (.NET 6+):46 ```csharp47 var services = new ServiceCollection();48 services.AddSingleton<ICustomerService, CustomerService>();49 services.AddTransient<MainForm>();50 using var sp = services.BuildServiceProvider();51 Application.Run(sp.GetRequiredService<MainForm>());52 ```534. **Use data binding** via `BindingSource` and `INotifyPropertyChanged` instead of manual control population. See [references/patterns.md](references/patterns.md) for complete binding patterns.545. **Use async/await** for I/O operations — disable controls during loading, use `Progress<T>` for progress reporting. Never block the UI thread.556. **Validate with `ErrorProvider`** and the `Validating` event. Call `ValidateChildren()` before save operations.567. **Modernize incrementally** — prefer better structure over big-bang rewrites. Use .NET 8+ features (button commands, stock icons) when available.5758## Current Upstream Notes5960- The August 2026 Windows Forms overview remains focused on Windows desktop, designer-driven controls, events, data binding, and migration to modern .NET. It does not change the framework-selection guidance: improve form boundaries and designer safety before proposing a rewrite.61- For docs-driven updates, validate whether the app targets .NET Framework, modern .NET, or mixed libraries before changing project format, designer files, or deployment assumptions.6263```mermaid64flowchart LR65 A["Form event"] --> B["Presenter handles logic"]66 B --> C["Service layer / data access"]67 C --> D["Update view via interface"]68 D --> E["Validate and display results"]69```7071## Key Decisions7273| Decision | Guidance |74|----------|----------|75| MVP vs MVVM | Prefer MVP for WinForms — simpler with event-driven model |76| BindingSource vs manual | Always prefer BindingSource for list/detail binding |77| Sync vs async I/O | Always async — use `async void` only for event handlers |78| Custom controls | Extract reusable `UserControl` when form grows beyond ~300 lines |79| .NET Framework → .NET | Use the official migration guide; validate designer compatibility first |8081## Deliver8283- less brittle form code with clear UI/logic separation84- MVP pattern with testable presenters85- pragmatic modernization guidance for WinForms-heavy apps86- data binding and validation patterns that reduce manual wiring8788## Validate8990- designer files stay stable and are not hand-edited91- forms are not acting as the application service layer92- async operations do not block the UI thread93- validation is implemented consistently with ErrorProvider94- Windows-only runtime behavior is tested on target9596## References9798- [references/patterns.md](references/patterns.md) - WinForms architectural patterns (MVP, MVVM, Passive View), data binding, validation, form communication, threading, DI setup, and .NET 8+ features99- [references/migration.md](references/migration.md) - step-by-step migration from .NET Framework to modern .NET, common issues, deployment options, and gradual migration strategies