Table of Contents

Method Prompt

Namespace
Terminal.Gui.Views
Assembly
Terminal.Gui.dll

Prompt<TView, TResult>(IRunnable, TView?, Func<TView, TResult?>?, TResult?, Action<Prompt<TView, TResult>>?)

Shows a view in a modal dialog with Ok/Cancel buttons and extracts a typed result.

public static TResult? Prompt<TView, TResult>(this IRunnable host, TView? view = null, Func<TView, TResult?>? resultExtractor = null, TResult? input = default, Action<Prompt<TView, TResult>>? beginInitHandler = null) where TView : View, new()

Parameters

host IRunnable

The runnable that is "hosting" this prompt. Currently used only to get the IApplication. In the future, this will enable positioning the prompt relative to the host.

view TView

The view to display in the dialog. If null, a new instance of TView is created.

resultExtractor Func<TView, TResult>

Function that extracts the result from the view when the user accepts. If null and TResult is string, automatically uses Text.

input TResult

Optional initial value to pass to the view (for future use with Input property).

beginInitHandler Action<Prompt<TView, TResult>>

Optional callback to customize the dialog before it is displayed.

Returns

TResult

The extracted result if the user accepted, or null if the user canceled.

Type Parameters

TView

The type of view to display.

TResult

The type of result data returned when the session completes.

Important: Use nullable types (e.g., Color?, int?, string?) so that null can indicate cancellation. Using non-nullable value types (e.g., Color, int) will return their default values on cancellation, making it impossible to distinguish cancellation from a valid result.

Examples

// From within a Window or other Runnable:
DateTime? date = this.Prompt<DatePicker, DateTime> (
                                                      view: new DatePicker { Date = new DateTime (1966, 9, 10) },
                                                      resultExtractor: dp => dp.Date);

if (date is { } selectedDate)
{
    MessageBox.Query ("Date Selected", $"You selected: {selectedDate:yyyy-MM-dd}", Strings.btnOk);
}

Remarks

The host parameter captures the "hosting relationship" between the caller and the prompt. Currently, this is used only to obtain the IApplication instance. In the future, this will enable prompts to be positioned relative to their host.

For detailed usage patterns and examples, see the Prompt Deep Dive.

Exceptions

InvalidOperationException

Thrown if host does not have an associated IApplication.

Prompt<TView>(IApplication, TView)

Shows a view in a modal dialog with Ok/Cancel buttons. For scripting languages.

public static string? Prompt<TView>(this IApplication app, TView view) where TView : View, new()

Parameters

app IApplication

The application instance.

view TView

The view to display in the dialog.

Returns

string

The text value from Text if the user accepted (clicked Ok), or null if canceled.

Type Parameters

TView

The type of view to display.

Examples

// PowerShell usage:
// $textField = [TextField]::new()
// $result = $app.Prompt($textField)
// if ($result) { Write-Output "User entered: $result" }

// C# usage:
TextField textField = new ();
string? result = app.Prompt (textField);

if (result is { })
{
    MessageBox.Query ("Input Received", $"You entered: {result}", Strings.btnOk);
}

Remarks

This overload is designed for scripting languages (PowerShell, F#, etc.) where Func<TView, TResult?> delegates are difficult to use.

Returns the string representation from Text. Many views provide meaningful Text implementations (e.g., ColorPicker returns the color name/value, DatePicker returns the formatted date).

For views where Text is not meaningful (e.g., ListView with multi-select), use the generic Prompt<TView, TResult>(IRunnable, TView?, Func<TView, TResult?>?, TResult?, Action<Prompt<TView, TResult>>?) method with a custom result extractor, or create Prompt<TView, TResult>(IRunnable, TView?, Func<TView, TResult?>?, TResult?, Action<Prompt<TView, TResult>>?) directly.

For PowerShell examples and detailed usage, see the Prompt Deep Dive.

Exceptions

ArgumentNullException

Thrown if app or view is null.