{"record":{"id":"44d41fba4541392a","repo":"tui-cs/Terminal.Gui","slug":"focuseditem-index-is-out-of-range","errorCode":null,"errorMessage":"FocusedItem index is out of range","messagePattern":"FocusedItem index is out of range","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Views/Selectors/OptionSelector.cs","lineNumber":231,"sourceCode":"                return 0;\n            }\n\n            return HasFocus ? SubViews.OfType<CheckBox> ().ToArray ().IndexOf (Focused) : field;\n        }\n        set\n        {\n            if (!CanFocus)\n            {\n                return;\n            }\n\n            field = value;\n\n            CheckBox [] checkBoxes = SubViews.OfType<CheckBox> ().ToArray ();\n\n            if (value < 0 || value >= checkBoxes.Length)\n            {\n                throw new ArgumentOutOfRangeException (nameof (value), @\"FocusedItem index is out of range\");\n            }\n\n            if (HasFocus)\n            {\n                checkBoxes [value].SetFocus ();\n            }\n        }\n    }\n\n    /// <inheritdoc/>\n    public bool EnableForDesign ()\n    {\n        AssignHotKeys = true;\n        Labels = [\"Option 1\", \"Option 2\", \"Third Option\", \"Option Quattro\"];\n\n        return true;\n    }\n","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Views/Selectors/OptionSelector.cs#L213-L249","documentation":"Thrown by OptionSelector.FocusedItem setter when CanFocus is true and the supplied value is < 0 or >= the number of CheckBox subviews. FocusedItem maps directly onto a checkbox in the SubViews, so an out-of-range index has no target to focus. Note the guard is skipped (early return) when CanFocus is false, so the throw only occurs in focusable selectors.","triggerScenarios":"Setting FocusedItem after the options/checkboxes changed (items removed), capturing an index from a previous layout, or computing the focused index from a search returning -1. Also setting it before the checkboxes are built.","commonSituations":"Re-binding the selector's source and reusing a stale focused index; filtering options without clamping focus; off-by-one using the options Count instead of Count-1.","solutions":["Clamp against the current checkbox count before assigning: selector.FocusedItem = Math.Clamp(idx, 0, checkboxCount - 1);","When rebuilding options, reset FocusedItem to 0 (or a valid index) afterwards.","Treat search/no-match (-1) as 'no focus change' instead of passing it in."],"exampleFix":"// before\nselector.FocusedItem = lastFocused; // stale after options changed\n\n// after\nvar count = selector.SubViews.OfType<CheckBox>().Count();\nselector.FocusedItem = count > 0 ? Math.Clamp(lastFocused, 0, count - 1) : 0;","handlingStrategy":"validation","validationCode":"int count = selector.SubViews.OfType<CheckBox>().Count();\nif (count > 0)\n{\n    selector.FocusedItem = Math.Clamp(idx, 0, count - 1);\n}","typeGuard":"static bool IsValidFocus(int i, int checkboxCount) => i >= 0 && i < checkboxCount;","tryCatchPattern":null,"preventionTips":["Reset FocusedItem to 0 when rebuilding options.","Clamp against the live checkbox count before assigning.","Treat search no-match (-1) as 'no change'."],"tags":["optionselector","index","validation","precondition"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}