{"record":{"id":"ab319c6230317bcd","repo":"tui-cs/Terminal.Gui","slug":"contentsize-cannot-be-negative","errorCode":null,"errorMessage":"ContentSize cannot be negative.","messagePattern":"ContentSize cannot be negative\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/ViewBase/View.Content.cs","lineNumber":121,"sourceCode":"    ///         currently visible to the user. This enables\n    ///         virtual scrolling and the behavior of <see cref=\"DimAutoStyle.Content\"/> will be to use\n    ///         <see cref=\"GetContentSize ()\"/> to determine the size\n    ///         of the view.\n    ///     </para>\n    ///     <para>\n    ///         This method follows the Cancellable Work Pattern (CWP). The <see cref=\"ContentSizeChanging\"/> event\n    ///         is raised before the change, and <see cref=\"ContentSizeChanged\"/> is raised after.\n    ///     </para>\n    /// </remarks>\n    /// <seealso cref=\"ContentSizeChanging\"/>\n    /// <seealso cref=\"ContentSizeChanged\"/>\n    /// <seealso cref=\"SetContentWidth\"/>\n    /// <seealso cref=\"SetContentHeight\"/>\n    public void SetContentSize (Size? contentSize)\n    {\n        if (contentSize is { } s && (s.Width < 0 || s.Height < 0))\n        {\n            throw new ArgumentException (@\"ContentSize cannot be negative.\", nameof (contentSize));\n        }\n\n        ApplyContentDimensionChange (contentSize?.Width, contentSize?.Height);\n    }\n\n    /// <summary>\n    ///     Internal helper that applies a content dimension change using the CWP pattern.\n    ///     Both dimensions are passed so the composite <see cref=\"Size\"/>? events fire correctly.\n    /// </summary>\n    private void ApplyContentDimensionChange (int? newWidth, int? newHeight)\n    {\n        // Compute old and new composite sizes for CWP events.\n        Size? oldComposite = _contentWidth is null && _contentHeight is null\n                                 ? null\n                                 : new Size (_contentWidth ?? Viewport.Size.Width, _contentHeight ?? Viewport.Size.Height);\n\n        Size? newComposite = newWidth is null && newHeight is null ? null : new Size (newWidth ?? Viewport.Size.Width, newHeight ?? Viewport.Size.Height);\n","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/ViewBase/View.Content.cs#L103-L139","documentation":"SetContentSize sets both width and height of the content area as a Size. Either dimension being negative is rejected — content space is always non-negative. Pass null to clear both and revert to Viewport tracking.","triggerScenarios":"Calling SetContentSize(new Size(-1, 10)), SetContentSize(new Size(w, h)) where w or h computed negative, or passing a Size built from scroll offsets that underflow.","commonSituations":"Scrolling logic that subtracts offsets from content bounds producing a negative remainder, deserializing a Size from config with invalid values, pre-layout computation when dimensions are unknown.","solutions":["Clamp both Width and Height to >= 0 before constructing the Size.","Pass null when you want the content to track the Viewport.","Use SetContentWidth/SetContentHeight individually if only one dimension is dynamic."],"exampleFix":"// before\nview.SetContentSize(new Size(offset - 5, height));\n// after\nview.SetContentSize(new Size(Math.Max(0, offset - 5), height));","handlingStrategy":"validation","validationCode":"if (size is { } sz && (sz.Width < 0 || sz.Height < 0)) size = null;\nview.SetContentSize (size);","typeGuard":"static bool IsValidContentSize (Size? s) => s is null || (s.Value.Width >= 0 && s.Value.Height >= 0);","tryCatchPattern":null,"preventionTips":["Validate both dimensions before constructing the Size.","Pass null to clear content sizing.","Guard scroll-offset arithmetic against underflow."],"tags":["content","layout","scrolling","validation"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}