tui-cs/Terminal.Gui · error · ArgumentOutOfRangeException

Result value must be a valid button index or null.

Error message

Result value must be a valid button index or null.

What it means

Dialog.Result stores the zero-based index of the button that was pressed (or null if dismissed without a button). Assigning a value outside [0, Buttons.Length) is rejected because it does not correspond to any real button in the dialog.

Source

Thrown at Terminal.Gui/Views/Dialog.cs:134

    /// </summary>
    /// <remarks>
    ///     <para>
    ///         The value is the zero-based index of the button that was pressed, or <see langword="null"/>
    ///         if the dialog was dismissed without a button press (e.g., via Escape key).
    ///     </para>
    ///     <para>
    ///         This property shadows the base <see cref="IRunnable{TResult}.Result"/> property to provide
    ///         explicit <c>int?</c> nullability for backward compatibility.
    ///     </para>
    /// </remarks>
    public new int? Result
    {
        get => ((IRunnable)this).Result is int value ? value : null;
        set
        {
            if (value >= Buttons.Length || value < 0)
            {
                throw new ArgumentOutOfRangeException (nameof (value), @"Result value must be a valid button index or null.");
            }

            ((IRunnable)this).Result = value;
        }
    }

    /// <summary>
    ///     Overrides the <see cref="Dialog{TResult}"/>  Activating behavior to handle non-Default Dialog Button presses.
    ///     The <see cref="View.DefaultAcceptView"/> button press is handled in <see cref="OnAccepting(CommandEventArgs)"/>.
    /// </summary>
    protected override bool OnActivating (CommandEventArgs args)
    {
        if (base.OnActivating (args))
        {
            return true;
        }

        if (args.Context?.Source?.TryGetTarget (out View? sourceView) is not true || !Buttons.Contains (sourceView as Button))

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Set Result only to a valid button index or null (dismissed).
  2. Recompute the index against dialog.Buttons.Length after adding/removing buttons.
  3. Use dialog.Buttons.IndexOf(targetButton) instead of a hard-coded number.

Example fix

// before
dialog.Result = 3; // only 2 buttons
// after
dialog.Result = dialog.Buttons.IndexOf(okButton);
Defensive patterns

Strategy: validation

Validate before calling

dialog.Result = (idx is null || (idx >= 0 && idx < dialog.Buttons.Length)) ? idx : null;

Type guard

static bool IsValidDialogResult (Dialog d, int? idx) => idx is null || (idx >= 0 && idx < d.Buttons.Length);

Prevention

When it happens

Trigger: Setting dialog.Result = 5 when only 3 buttons exist; passing a negative index; computing an index from a removed/added button without updating the count.

Common situations: Dynamically adding/removing buttons but caching an old index; setting Result before buttons are configured; index arithmetic that overshoots.

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/aa6f6db3f5bd98c1. Report an issue: GitHub.