dotnet/wpf · error · ArgumentException

SR.PrintDialogInvalidPageRange

Error message

SR.PrintDialogInvalidPageRange

What it means

PrintDialog.PageRange setter validates PageRange before storing it: either PageFrom or PageTo being <= 0 raises ArgumentException with SR.PrintDialogInvalidPageRange. Page numbers are 1-based; zero or negative bounds are meaningless print ranges. The stored range is then further normalized/validated (From > To handling follows).

Solutions

  1. Validate that both PageFrom and PageTo are >= 1 before assigning PageRange.
  2. Clamp or default invalid input: if from < 1, use 1; ensure from <= to or swap/normalize.
  3. If 'print all' was intended, leave PageRange unset or set UserPageRangeEnabled = false instead of using 0.

Example fix

// before
dialog.PageRange = new PageRange(userFrom, userTo); // userFrom may be 0 -> throws
// after
int from = Math.Max(1, userFrom);
int to = Math.Max(from, userTo);
dialog.PageRange = new PageRange(from, to);
Defensive patterns

Strategy: validation

Validate before calling

bool valid = range.PageFrom >= 1 && range.PageTo >= 1;
if (valid) printDialog.PageRange = range;

Type guard

static bool IsValidPageRange(PageRange r) => r.PageFrom >= 1 && r.PageTo >= 1;

Try / catch

try
{
    printDialog.PageRange = range;
}
catch (ArgumentException)
{
    // invalid page numbers from user input; fall back to full-document print
}

Prevention

When it happens

Trigger: Assigning printDialog.PageRange = new PageRange(0, 10) or (5, 0) or any negative value; copying a PageRange from user input where 'all' was encoded as 0.

Common situations: Print dialogs or automation scripts reading page numbers from UI text boxes without parsing validation; defaults initialized to 0 instead of 1.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/aecb655272de6a6a. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/PrintDialog.cs:83

                _pageRangeSelection = value;
            }
        }

        /// <summary>
        /// Gets or sets a PageRange objects used when the PageRangeSelection
        /// option is set to UserPages.
        /// </summary>
        public PageRange PageRange
        {
            get
            {
                return _pageRange;
            }
            set
            {
                if ((value.PageTo <= 0) || (value.PageFrom <= 0))
                {
                    throw new System.ArgumentException(SR.PrintDialogInvalidPageRange, "PageRange");
                }

                _pageRange = value;

                if (_pageRange.PageFrom > _pageRange.PageTo)
                {
                    int temp = _pageRange.PageFrom;
                    _pageRange.PageFrom = _pageRange.PageTo;
                    _pageRange.PageTo = temp;
                }
            }
        }

        /// <summary>
        /// Gets or a sets a flag to enable/disable the user page range support on
        /// the print dialog.
        /// </summary>
        public bool UserPageRangeEnabled

View on GitHub (pinned to 81131a70a4)