Manage Form lifetime and navigation
Make window ownership, instance state and closure behavior intentional.
Choose the instance deliberately#
Use New when you need a separate instance with independent state. A named Form type can also expose a lazy default instance, but mixing the two approaches makes lifetime harder to reason about. In a list/detail workflow, pass the selected identity into the exact detail instance you will show.
Put work in the right phase#
Use the Form event reference for the supported lifecycle events. Initialization, loading and first presentation are different phases. If code needs the visible UI and awaits external work, an Async Shown handler is a natural place to begin. Do not assume a repeated Show always means a new instance.
The multiple-Forms lesson records lifecycle events so you can observe show, hide and unload rather than memorize a guessed event order.
Distinguish modeless, modal and MDI#
| Mode | User experience | Implementation concern |
|---|---|---|
| Modeless | Several windows can be used independently. | Keep per-instance state and ownership clear. |
| Modal | The user finishes or cancels a focused task before returning to the owner. | Await the result from an Async handler; preserve cancellation. |
| MDI | Related documents live in a workspace. | Distinguish child documents from independent windows. |
See ModalDialog and MdiForms for complete projects.
Make closing safe during work#
A Form with an in-flight write or download must decide what closure means. QueryUnload can postpone closing while cancellation completes. Finally restores state and can perform the delayed Unload. Release owned database or cancellation resources; do not leave a timer polling a disposed operation.
20f4bfed6b · Build manifest