dotnet/wpf · error · ArgumentException

SR.TextRangeProvider_EmptyStringParameter

Error message

SR.TextRangeProvider_EmptyStringParameter

What it means

TextRangeAdaptor implements UI Automation's ITextRangeProvider for WPF text controls. Its FindText method throws ArgumentException (SR.TextRangeProvider_EmptyStringParameter) when the search text is an empty string, because searching for an empty substring is meaningless. The library enforces non-null via ArgumentNullException.ThrowIfNull and non-empty via this check to give a clear error instead of undefined behavior.

Solutions

  1. Ensure the search string is non-empty before calling FindText; skip the call or return no match when text.Length == 0.
  2. If the string comes from user input, validate/trim it first and fall back to a different behavior (e.g. no filtering) when empty.
  3. Catch ArgumentException around FindText in generic automation wrappers and treat it as 'no match' if an empty query is expected to be harmless.

Example fix

// before
range.FindText(userQuery, false, false);

// after
if (!string.IsNullOrEmpty(userQuery))
{
    range.FindText(userQuery, false, false);
}
else
{
    // treat empty query as 'no search'
}
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(text)) throw new ArgumentOutOfRangeException(nameof(text)); // or skip the search
range.FindText(text, backward, ignoreCase);

Type guard

static bool IsValidSearchText(string? s) => !string.IsNullOrEmpty(s);

Prevention

When it happens

Trigger: Calling ITextRangeProvider.FindText(text, backward, ignoreCase) on a WPF text range (obtained via UI Automation, e.g. from a TextBlock/TextBox/TextBoxBase automation peer) with text == "" (zero-length string).

Common situations: UI Automation clients that build search text dynamically from user input or a filter box and call FindText before validating the input; localization/unit tests passing default empty strings; truncation logic yielding an empty query.

Understand the failure class

Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/TextRangeAdaptor.cs:1751

                resultRange = new TextRangeAdaptor(_textAdaptor, attrStart, attrEnd, _textPeer);
            }

            return resultRange;
        }

        /// <summary>
        /// Searches for an occurrence of text within the range.
        /// </summary>
        /// <param name="text">The text to search for.</param>
        /// <param name="backward">true if the last occurring range should be returned instead of the first.</param>
        /// <param name="ignoreCase">true if case should be ignored for the purposes of comparison.</param>
        /// <returns>A subrange with the specified text, or null if no such subrange exists.</returns>
        ITextRangeProvider ITextRangeProvider.FindText(string text, bool backward, bool ignoreCase)
        {
            ArgumentNullException.ThrowIfNull(text);
            if (text.Length == 0)
            {
                throw new ArgumentException(SR.Format(SR.TextRangeProvider_EmptyStringParameter, "text"));
            }

            Normalize();

            if (_start.CompareTo(_end) == 0)
            {
                return null;
            }

            TextRangeAdaptor range = null;
            FindFlags findFlags = FindFlags.None;
            if (!ignoreCase)
            {
                findFlags |= FindFlags.MatchCase;
            }
            if (backward)
            {
                findFlags |= FindFlags.FindInReverse;

View on GitHub (pinned to 81131a70a4)