{"record":{"id":"aa6f6db3f5bd98c1","repo":"tui-cs/Terminal.Gui","slug":"result-value-must-be-a-valid-button-index-or-null","errorCode":null,"errorMessage":"Result value must be a valid button index or null.","messagePattern":"Result value must be a valid button index or null\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/Views/Dialog.cs","lineNumber":134,"sourceCode":"    /// </summary>\n    /// <remarks>\n    ///     <para>\n    ///         The value is the zero-based index of the button that was pressed, or <see langword=\"null\"/>\n    ///         if the dialog was dismissed without a button press (e.g., via Escape key).\n    ///     </para>\n    ///     <para>\n    ///         This property shadows the base <see cref=\"IRunnable{TResult}.Result\"/> property to provide\n    ///         explicit <c>int?</c> nullability for backward compatibility.\n    ///     </para>\n    /// </remarks>\n    public new int? Result\n    {\n        get => ((IRunnable)this).Result is int value ? value : null;\n        set\n        {\n            if (value >= Buttons.Length || value < 0)\n            {\n                throw new ArgumentOutOfRangeException (nameof (value), @\"Result value must be a valid button index or null.\");\n            }\n\n            ((IRunnable)this).Result = value;\n        }\n    }\n\n    /// <summary>\n    ///     Overrides the <see cref=\"Dialog{TResult}\"/>  Activating behavior to handle non-Default Dialog Button presses.\n    ///     The <see cref=\"View.DefaultAcceptView\"/> button press is handled in <see cref=\"OnAccepting(CommandEventArgs)\"/>.\n    /// </summary>\n    protected override bool OnActivating (CommandEventArgs args)\n    {\n        if (base.OnActivating (args))\n        {\n            return true;\n        }\n\n        if (args.Context?.Source?.TryGetTarget (out View? sourceView) is not true || !Buttons.Contains (sourceView as Button))","sourceCodeStart":116,"sourceCodeEnd":152,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/Views/Dialog.cs#L116-L152","documentation":"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.","triggerScenarios":"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.","commonSituations":"Dynamically adding/removing buttons but caching an old index; setting Result before buttons are configured; index arithmetic that overshoots.","solutions":["Set Result only to a valid button index or null (dismissed).","Recompute the index against dialog.Buttons.Length after adding/removing buttons.","Use dialog.Buttons.IndexOf(targetButton) instead of a hard-coded number."],"exampleFix":"// before\ndialog.Result = 3; // only 2 buttons\n// after\ndialog.Result = dialog.Buttons.IndexOf(okButton);","handlingStrategy":"validation","validationCode":"dialog.Result = (idx is null || (idx >= 0 && idx < dialog.Buttons.Length)) ? idx : null;","typeGuard":"static bool IsValidDialogResult (Dialog d, int? idx) => idx is null || (idx >= 0 && idx < d.Buttons.Length);","tryCatchPattern":null,"preventionTips":["Recompute index after adding/removing buttons.","Use Buttons.IndexOf(button) not hard-coded numbers.","Set null for 'dismissed'.","Always validate the index against Buttons.Length"],"tags":["dialog","buttons","result","validation"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}