tui-cs/Terminal.Gui · error · ArgumentException

start must be greater than or equal to 0

Error message

start must be greater than or equal to 0

What it means

Thrown by Ruler.Draw when the start parameter is negative. The ruler draws from a template substring indexed by start, so a negative index is invalid. It is an ArgumentException. The Ruler class is internal, so this is reached via Terminal.Gui's own rendering helpers rather than directly by application code.

Source

Thrown at Terminal.Gui/Drawing/Ruler.cs:32

    public int Length { get; set; }

    /// <summary>Gets or sets whether the ruler is drawn horizontally or vertically. The default is horizontally.</summary>
    public Orientation Orientation { get; set; }

    private string _hTemplate { get; } = "|123456789";
    private string _vTemplate { get; } = "-123456789";

    /// <summary>Draws the <see cref="Ruler"/>.</summary>
    /// <param name="driver">Optional Driver. If not provided, driver will be used.</param>
    /// <param name="location">The location to start drawing the ruler, in screen-relative coordinates.</param>
    /// <param name="start">The start value of the ruler.</param>
    public void Draw (IDriver? driver, Point location, int start = 0)
    {
        ArgumentNullException.ThrowIfNull (driver);

        if (start < 0)
        {
            throw new ArgumentException ("start must be greater than or equal to 0");
        }

        if (Length < 1)
        {
            return;
        }

        if (Orientation == Orientation.Horizontal)
        {
            string hrule =
                _hTemplate.Repeat ((int)Math.Ceiling (Length + 2 / (double)_hTemplate.Length))! [start..(Length + start)];

            // Top
            driver?.Move (location.X, location.Y);
            driver?.AddStr (hrule);
        }
        else
        {

View on GitHub (pinned to 2e47b11478)

Solutions

  1. Ensure start >= 0 before calling Draw (clamp to 0).
  2. Guard upstream offsets so they never go negative.
  3. Return early when Length < 1 (Draw already does) to avoid drawing with bad offsets.

Example fix

// before
ruler.Draw(driver, location, start: offset);

// after
ruler.Draw(driver, location, start: Math.Max(0, offset));
Defensive patterns

Strategy: validation

Validate before calling

int s = Math.Max(0, offset);

Type guard

static bool IsValidRulerStart(int start) => start >= 0;

Try / catch

try { ruler.Draw(driver, location, start); } catch (ArgumentException) { ruler.Draw(driver, location, 0); }

Prevention

When it happens

Trigger: Calling ruler.Draw(driver, location, start: -1) or passing a computed start that underflows below zero.

Common situations: Internal callers computing a scroll/offset start that goes negative on small viewports, or negative coordinates from layout arithmetic.

Related errors


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