tui-cs/Terminal.Gui · error · InvalidOperationException

CheckBox value not found

Error message

CheckBox value not found

What it means

Thrown by SelectorBase.GetCheckBoxValue when the passed CheckBox is not a key in the internal _checkBoxValues map — i.e. the checkbox does not belong to this selector. The map is the source of truth for the integer each checkbox represents, so a foreign checkbox has no value to return. Intended for testing/advanced scenarios per its doc comment.

Source

Thrown at Terminal.Gui/Views/Selectors/SelectorBase.cs:485

        _checkBoxValues [checkbox] = value;

        return checkbox;
    }

    /// <summary>
    ///     Gets the int value associated with a checkbox. For testing and advanced scenarios.
    /// </summary>
    /// <param name="checkbox">The checkbox to get the value for</param>
    /// <returns>The integer value associated with the checkbox</returns>
    /// <exception cref="InvalidOperationException">If the checkbox is not found or not part of this selector</exception>
    public int GetCheckBoxValue (CheckBox checkbox)
    {
        if (_checkBoxValues.TryGetValue (checkbox, out int value))
        {
            return value;
        }

        throw new InvalidOperationException ("CheckBox value not found");
    }

    private int _horizontalSpace = 2;

    /// <summary>
    ///     Gets or sets the horizontal space for this <see cref="OptionSelector"/> if the <see cref="Orientation"/> is
    ///     <see cref="Orientation.Horizontal"/>
    /// </summary>
    public int HorizontalSpace
    {
        get => _horizontalSpace;
        set
        {
            if (_horizontalSpace == value)
            {
                return;
            }
            _horizontalSpace = value;

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Only pass checkboxes obtained from this selector's own SubViews and only while its options are unchanged.
  2. Before calling, check membership via the selector's own subviews: if (selector.SubViews.OfType<CheckBox>().Contains(cb)) ...
  3. Re-acquire checkbox references whenever Options/Values are rebuilt.

Example fix

// before
int v = selector.GetCheckBoxValue(maybeForeignCheckbox); // throws 173

// after
if (selector.SubViews.OfType<CheckBox>().Contains(maybeForeignCheckbox))
{
    int v = selector.GetCheckBoxValue(maybeForeignCheckbox);
}
Defensive patterns

Strategy: type-guard

Validate before calling

if (selector.SubViews.OfType<CheckBox>().Contains(cb))
{
    int v = selector.GetCheckBoxValue(cb);
}

Type guard

static bool CheckBoxBelongsTo(SelectorBase s, CheckBox cb) =>
    s.SubViews.OfType<CheckBox>().Contains(cb);

Prevention

When it happens

Trigger: Passing a CheckBox from a different selector, a manually new'd CheckBox, or a checkbox obtained before the selector built its internal map. Also calling it after the selector was rebuilt/rebound so old checkbox references are stale.

Common situations: Unit tests passing an arbitrary CheckBox; holding checkbox references across a rebuild of the selector's options; mixing checkboxes from two selectors in shared handler code.

Related errors


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