tui-cs/Terminal.Gui · error · ArgumentException
Content width cannot be negative.
Error message
Content width cannot be negative.
What it means
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.
Source
Thrown at Terminal.Gui/ViewBase/View.Content.cs:42
/// If set to <see langword="null"/>, <see cref="GetContentWidth ()"/> will track the <see cref="Viewport"/> width.
/// </para>
/// <para>
/// If set to a non-<see langword="null"/> value, the content width is independent of the <see cref="Viewport"/>
/// width, enabling horizontal scrolling.
/// </para>
/// <para>
/// This method follows the Cancellable Work Pattern (CWP). The <see cref="ContentSizeChanging"/> event
/// is raised before the change, and <see cref="ContentSizeChanged"/> is raised after.
/// </para>
/// </remarks>
/// <param name="contentWidth">The new content width, or <see langword="null"/> to track the Viewport width.</param>
/// <seealso cref="SetContentHeight"/>
/// <seealso cref="SetContentSize"/>
public void SetContentWidth (int? contentWidth)
{
if (contentWidth is < 0)
{
throw new ArgumentException (@"Content width cannot be negative.", nameof (contentWidth));
}
ApplyContentDimensionChange (contentWidth, _contentHeight);
}
/// <summary>
/// Sets the height of the View's content area independently of the width.
/// </summary>
/// <remarks>
/// <para>
/// See the View Layout Deep Dive for more information:
/// <see href="https://tui-cs.github.io/Terminal.Gui/docs/layout.html"/>
/// </para>
/// <para>
/// Negative values are not supported.
/// </para>
/// <para>
/// If set to <see langword="null"/>, <see cref="GetContentHeight ()"/> will track the <see cref="Viewport"/>View on GitHub (pinned to 2e47b11478)
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'.
Example fix
// before view.SetContentWidth(viewportWidth - padding); // after view.SetContentWidth(Math.Max(0, viewportWidth - padding));
Defensive patterns
Strategy: validation
Validate before calling
int? w = computedWidth < 0 ? throw new ArgumentOutOfRangeException(nameof(computedWidth)) : (int?)computedWidth; // or simply: view.SetContentWidth(computedWidth >= 0 ? computedWidth : null);
Type guard
static bool IsValidContentDim (int? d) => d is null || d >= 0;
Prevention
- Clamp computed widths to >= 0.
- Pass null to mean 'track Viewport'.
- Avoid using -1 as a sentinel.
When it happens
Trigger: 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).
Common situations: Calculating content size before layout completes (Viewport is 0), off-by-one in scroll math, porting code that used -1 as a sentinel.
Related errors
- Content height cannot be negative.
- ContentSize cannot be negative.
- The size of an item cannot be negative.
- X cannot be null
- Y cannot be null
AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13).
Data as JSON: /api/errors/7e78a31e470cad1d.
Report an issue: GitHub.