{"record":{"id":"63acf38f859d38b3","repo":"tui-cs/Terminal.Gui","slug":"the-size-of-an-item-cannot-be-negative","errorCode":null,"errorMessage":"The size of an item cannot be negative.","messagePattern":"The size of an item cannot be negative\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Terminal.Gui/ViewBase/Layout/Aligner.cs","lineNumber":365,"sourceCode":"        int currentPosition = containerSize - totalItemsSize - spacesToGive;\n\n        for (var i = 0; i < sizes.Length; i++)\n        {\n            CheckSizeCannotBeNegative (i, in sizes);\n            int spaceBefore = spacesToGive-- > 0 ? maxSpaceBetweenItems : 0;\n\n            positions [i] = currentPosition;\n            currentPosition += sizes [i] + spaceBefore;\n        }\n\n        return positions;\n    }\n\n    private static void CheckSizeCannotBeNegative (int i, ref readonly int [] sizes)\n    {\n        if (sizes [i] < 0)\n        {\n            throw new ArgumentException (\"The size of an item cannot be negative.\");\n        }\n    }\n}\n","sourceCodeStart":347,"sourceCodeEnd":369,"githubUrl":"https://github.com/tui-cs/Terminal.Gui/blob/2e47b11478db083499917f2ad27c34d30efb0df1/Terminal.Gui/ViewBase/Layout/Aligner.cs#L347-L369","documentation":"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.","triggerScenarios":"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.","commonSituations":"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).","solutions":["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.","Ensure the container is large enough for all SubViews before triggering layout.","Debug by logging the resolved Frame/Viewport sizes of SubViews before layout.","Remove or fix any Dim.Absolute with negative values."],"exampleFix":"// before -- Dim.Fill can go negative in small containers\nview.Add(new Label { Width = Dim.Fill(100) });\n\n// after -- use a reasonable fill margin\nview.Add(new Label { Width = Dim.Fill(1) });","handlingStrategy":"validation","validationCode":"// Check resolved sizes before layout\nforeach (var sub in view.Subviews) { if (sub.Frame.Width < 0 || sub.Frame.Height < 0) { /* fix Dim config */ } }","typeGuard":null,"tryCatchPattern":"// Aligner methods are internal; wrap layout-triggering code\ntry { view.SetNeedsLayout(); app.LayoutAndDraw(); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"negative\"))\n{ /* inspect Subview Dim configurations for overflow */ }","preventionTips":["Avoid Dim.Fill(n) where n could exceed the available container space.","Ensure containers are large enough for their SubViews before layout.","Do not use negative Dim.Absolute values.","Log resolved Frame dimensions of SubViews during development to catch layout issues early."],"tags":["layout","alignment","viewbase","dim","validation"],"backgroundTag":null,"analyzedSha":"2e47b11478db083499917f2ad27c34d30efb0df1","analyzedAt":"2026-08-13T19:20:08.826Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}