tui-cs/Terminal.Gui · error · ArgumentException

The size of an item cannot be negative.

Error message

The size of an item cannot be negative.

What it means

This error is thrown by Aligner.CheckSizeCannotBeNegative() when a layout item has a negative size value during alignment calculations. The Aligner class computes positions for items arranged with alignment (Start, Center, End, Fill) within a container. Negative sizes are logically invalid -- they would mean an item takes negative space -- and indicate a bug in layout computation, typically caused by a View with a negative width/height or a corrupted Dim computation.

Source

Thrown at Terminal.Gui/ViewBase/Layout/Aligner.cs:365

        int currentPosition = containerSize - totalItemsSize - spacesToGive;

        for (var i = 0; i < sizes.Length; i++)
        {
            CheckSizeCannotBeNegative (i, in sizes);
            int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;

            positions [i] = currentPosition;
            currentPosition += sizes [i] + spaceBefore;
        }

        return positions;
    }

    private static void CheckSizeCannotBeNegative (int i, ref readonly int [] sizes)
    {
        if (sizes [i] < 0)
        {
            throw new ArgumentException ("The size of an item cannot be negative.");
        }
    }
}

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Check the views being laid out for Dim configurations that could resolve negative -- particularly Dim.Fill with large margins or Dim.Percent on tiny containers.
  2. Ensure the container is large enough for all SubViews before triggering layout.
  3. Debug by logging the resolved Frame/Viewport sizes of SubViews before layout.
  4. Remove or fix any Dim.Absolute with negative values.

Example fix

// before -- Dim.Fill can go negative in small containers
view.Add(new Label { Width = Dim.Fill(100) });

// after -- use a reasonable fill margin
view.Add(new Label { Width = Dim.Fill(1) });
Defensive patterns

Strategy: validation

Validate before calling

// Check resolved sizes before layout
foreach (var sub in view.Subviews) { if (sub.Frame.Width < 0 || sub.Frame.Height < 0) { /* fix Dim config */ } }

Try / catch

// Aligner methods are internal; wrap layout-triggering code
try { view.SetNeedsLayout(); app.LayoutAndDraw(); }
catch (ArgumentException ex) when (ex.Message.Contains("negative"))
{ /* inspect Subview Dim configurations for overflow */ }

Prevention

When it happens

Trigger: Thrown at Aligner.cs:365 when sizes[i] < 0 during Start(), Center(), or End() alignment methods. The sizes array represents the computed widths (horizontal) or heights (vertical) of items being laid out. A negative entry means a Dim resolved to a negative value, which is always a bug.

Common situations: A View with Dim.Fill(n) where n exceeds the available space producing a negative resolved dimension, a View with a hardcoded negative Dim.Absolute, circular layout dependencies causing undersized containers, or layout being triggered before all views are properly sized (e.g., during construction).

Related errors


AI-assisted analysis of tui-cs/Terminal.Gui@2e47b11478 (2026-08-13). Data as JSON: /api/errors/63acf38f859d38b3. Report an issue: GitHub.