Class Dialog<TResult>
A generic modal dialog window with buttons across the bottom. Derive from this class to create dialogs that return custom result types.
public class Dialog<TResult> : Runnable<TResult>, IDisposable, ISupportInitializeNotification, ISupportInitialize, IRunnable<TResult>, IRunnable, IDesignable
Type Parameters
TResultThe type of result data returned when the dialog closes. Since Result is
TResult?, use non-nullable types (e.g.,stringnotstring?) to allownullto indicate cancellation.
- Inheritance
-
Runnable<TResult>Dialog<TResult>
- Implements
-
IRunnable<TResult>
- Derived
- Inherited Members
- Extension Methods
Examples
// Custom dialog returning a Color
public class ColorDialog : Dialog<Color>
{
private ColorPicker _picker;
public ColorDialog (Color initialColor)
{
_picker = new () { Value = initialColor };
Add (_picker);
AddButton (new () { Text = "_Cancel" });
AddButton (new () { Text = "_Ok" });
}
protected override bool OnAccepting (CommandEventArgs args)
{
if (base.OnAccepting (args))
{
return true;
}
Result = _picker.Value;
RequestStop ();
return false;
}
}
Remarks
By default, Dialog<TResult> is centered with Auto(DimAutoStyle, Dim?, Dim?) sizing and uses the Dialog color scheme when running.
To run modally, pass the dialog to Run(IRunnable, Func<Exception, bool>?). The dialog executes until terminated by QuitKey (Esc by default), a press of one of the Buttons, or if any subview receives the Accept command and does not handle it.
Buttons are added via AddButton(Button) or the Buttons property. The last button added becomes the default (IsDefault). Button alignment is controlled by ButtonAlignment and ButtonAlignmentModes.
Subclasses should set Result before calling RequestStop()
to return a value. If Result is not set (remains null), the dialog is considered canceled.
Constructors
- Dialog()
Initializes a new instance of the Dialog<TResult> class with no buttons.
Fields
- _buttonContainer
The container view that holds the dialog buttons in the Padding area.
Properties
- ButtonAlignment
Determines how buttons are aligned horizontally at the bottom of the dialog.
- ButtonAlignmentModes
Controls button spacing and alignment behavior.
- Buttons
Gets or sets the buttons displayed at the bottom of the dialog.
Methods
- AddButton(Button)
Adds a Button to the bottom of the dialog and to the Buttons collection.
- EndInit()
Signals the View that initialization is ending. See ISupportInitialize.
- OnAccepting(CommandEventArgs)
Overrides the default Accepting behavior to handle Dialog Button presses.
- OnDrawingText()
Called when the Text of the View is to be drawn.
- OnDrewText()
Called when the Text of the View has been drawn.
- OnGettingAttributeForRole(in VisualRole, ref Attribute)
Called when the Attribute for a GetAttributeForRole(VisualRole) is being retrieved. Implementations can return true to stop further processing and optionally set the Attribute in the event args to a different value.
- OnIsRunningChanged(bool)
Called after IsRunning has changed. Override for post-state-change logic.
- OnSubViewAdded(View)
Called when a SubView has been added to this View.
- OnSubViewLayout(LayoutEventArgs)
Called from Terminal.Gui.ViewBase.View.LayoutSubViews before any subviews have been laid out.