tui-cs/Terminal.Gui · error · ArgumentException
Content height cannot be negative.
Error message
Content height cannot be negative.
What it means
SetContentHeight sets the height of the View's content area independently of the Viewport, enabling vertical scrolling. A negative height is rejected because content cannot occupy negative rows. Pass null to track the Viewport height automatically.
Source
Thrown at Terminal.Gui/ViewBase/View.Content.cs:79
/// height.
/// </para>
/// <para>
/// If set to a non-<see langword="null"/> value, the content height is independent of the <see cref="Viewport"/>
/// height, enabling vertical 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="contentHeight">The new content height, or <see langword="null"/> to track the Viewport height.</param>
/// <seealso cref="SetContentWidth"/>
/// <seealso cref="SetContentSize"/>
public void SetContentHeight (int? contentHeight)
{
if (contentHeight is < 0)
{
throw new ArgumentException (@"Content height cannot be negative.", nameof (contentHeight));
}
ApplyContentDimensionChange (_contentWidth, contentHeight);
}
/// <summary>
/// Sets the size of the View's content.
/// </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 sizes are not supported.
/// </para>
/// <para>
/// If not explicitly set to a non-<see langword="null"/> value, and the View has Subviews,View on GitHub (pinned to 2e47b11478)
Solutions
- Clamp the computed height to zero minimum.
- Pass null to let the height track the Viewport.
- Call SetContentSize/Height only after the Viewport is laid out.
Example fix
// before view.SetContentHeight(lines * -1); // after view.SetContentHeight(Math.Max(0, lines));
Defensive patterns
Strategy: validation
Validate before calling
view.SetContentHeight (h >= 0 ? h : null);
Type guard
static bool IsValidContentDim (int? d) => d is null || d >= 0;
Prevention
- Clamp computed heights to >= 0.
- Use null for Viewport-tracking mode.
- Defer content sizing until after layout.
When it happens
Trigger: Calling SetContentHeight(-1) or passing a computed height that goes negative (e.g. lineHeight * itemCount where itemCount is -1, or subtracting a header from an uninitialized Viewport).
Common situations: Pre-layout content sizing when Viewport is still 0, scroll-offset arithmetic that subtracts too much, using -1 as an 'unset' sentinel.
Related errors
- Content width 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/526f7cb8a2ba219e.
Report an issue: GitHub.