{"record":{"id":"7e78a31e470cad1d","repo":"tui-cs/Terminal.Gui","slug":"content-width-cannot-be-negative","errorCode":null,"errorMessage":"Content width cannot be negative.","messagePattern":"Content width cannot be negative\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/ViewBase/View.Content.cs","lineNumber":42,"sourceCode":"    ///         If set to <see langword=\"null\"/>, <see cref=\"GetContentWidth ()\"/> will track the <see cref=\"Viewport\"/> width.\n    ///     </para>\n    ///     <para>\n    ///         If set to a non-<see langword=\"null\"/> value, the content width is independent of the <see cref=\"Viewport\"/>\n    ///         width, enabling horizontal scrolling.\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    /// <param name=\"contentWidth\">The new content width, or <see langword=\"null\"/> to track the Viewport width.</param>\n    /// <seealso cref=\"SetContentHeight\"/>\n    /// <seealso cref=\"SetContentSize\"/>\n    public void SetContentWidth (int? contentWidth)\n    {\n        if (contentWidth is < 0)\n        {\n            throw new ArgumentException (@\"Content width cannot be negative.\", nameof (contentWidth));\n        }\n\n        ApplyContentDimensionChange (contentWidth, _contentHeight);\n    }\n\n    /// <summary>\n    ///     Sets the height of the View's content area independently of the width.\n    /// </summary>\n    /// <remarks>\n    ///     <para>\n    ///         See the View Layout Deep Dive for more information:\n    ///         <see href=\"https://tui-cs.github.io/Terminal.Gui/docs/layout.html\"/>\n    ///     </para>\n    ///     <para>\n    ///         Negative values are not supported.\n    ///     </para>\n    ///     <para>\n    ///         If set to <see langword=\"null\"/>, <see cref=\"GetContentHeight ()\"/> will track the <see cref=\"Viewport\"/>","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/ViewBase/View.Content.cs#L24-L60","documentation":"SetContentWidth sets the width of the View's content area independently of the Viewport, enabling horizontal scrolling. A negative width is meaningless (content cannot occupy negative columns), so the library rejects it. Pass null to revert to tracking the Viewport width automatically.","triggerScenarios":"Calling SetContentWidth(-1) or SetContentWidth with a computed int that underflows to a negative value (e.g. subtracting a margin from an as-yet-uninitialized Viewport width).","commonSituations":"Calculating content size before layout completes (Viewport is 0), off-by-one in scroll math, porting code that used -1 as a sentinel.","solutions":["Clamp the value to zero or pass null instead of a negative number.","Defer the call until after layout (EndInit) so Viewport.Width is known.","Pass null explicitly to mean 'track the Viewport'."],"exampleFix":"// before\nview.SetContentWidth(viewportWidth - padding);\n// after\nview.SetContentWidth(Math.Max(0, viewportWidth - padding));","handlingStrategy":"validation","validationCode":"int? w = computedWidth < 0 ? throw new ArgumentOutOfRangeException(nameof(computedWidth)) : (int?)computedWidth;\n// or simply: view.SetContentWidth(computedWidth >= 0 ? computedWidth : null);","typeGuard":"static bool IsValidContentDim (int? d) => d is null || d >= 0;","tryCatchPattern":null,"preventionTips":["Clamp computed widths to >= 0.","Pass null to mean 'track Viewport'.","Avoid using -1 as a sentinel."],"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"}