tui-cs/Terminal.Gui · error · ArgumentException
ContentSize cannot be negative.
Error message
ContentSize cannot be negative.
What it means
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.
Source
Thrown at Terminal.Gui/ViewBase/View.Content.cs:121
/// currently visible to the user. This enables
/// virtual scrolling and the behavior of <see cref="DimAutoStyle.Content"/> will be to use
/// <see cref="GetContentSize ()"/> to determine the size
/// of the view.
/// </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>
/// <seealso cref="ContentSizeChanging"/>
/// <seealso cref="ContentSizeChanged"/>
/// <seealso cref="SetContentWidth"/>
/// <seealso cref="SetContentHeight"/>
public void SetContentSize (Size? contentSize)
{
if (contentSize is { } s && (s.Width < 0 || s.Height < 0))
{
throw new ArgumentException (@"ContentSize cannot be negative.", nameof (contentSize));
}
ApplyContentDimensionChange (contentSize?.Width, contentSize?.Height);
}
/// <summary>
/// Internal helper that applies a content dimension change using the CWP pattern.
/// Both dimensions are passed so the composite <see cref="Size"/>? events fire correctly.
/// </summary>
private void ApplyContentDimensionChange (int? newWidth, int? newHeight)
{
// Compute old and new composite sizes for CWP events.
Size? oldComposite = _contentWidth is null && _contentHeight is null
? null
: new Size (_contentWidth ?? Viewport.Size.Width, _contentHeight ?? Viewport.Size.Height);
Size? newComposite = newWidth is null && newHeight is null ? null : new Size (newWidth ?? Viewport.Size.Width, newHeight ?? Viewport.Size.Height);
View on GitHub (pinned to 2e47b11478)
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.
Example fix
// before view.SetContentSize(new Size(offset - 5, height)); // after view.SetContentSize(new Size(Math.Max(0, offset - 5), height));
Defensive patterns
Strategy: validation
Validate before calling
if (size is { } sz && (sz.Width < 0 || sz.Height < 0)) size = null;
view.SetContentSize (size); Type guard
static bool IsValidContentSize (Size? s) => s is null || (s.Value.Width >= 0 && s.Value.Height >= 0);
Prevention
- Validate both dimensions before constructing the Size.
- Pass null to clear content sizing.
- Guard scroll-offset arithmetic against underflow.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- Content width cannot be negative.
- Content height 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/ab319c6230317bcd.
Report an issue: GitHub.