dotnet/wpf · error · ArgumentException
SR.Format(SR.NotAValidValue, str)
Error message
SR.Format(SR.NotAValidValue, str)
What it means
When a rich edit control has the ES_NUMBER style, SetValue validates that the new string contains no alphabetic characters before sending it. A string containing letters is not a valid numeric value for that control, so it throws ArgumentException(SR.Format(SR.NotAValidValue, str)) with parameter name "val".
Solutions
- Strip or correct non-numeric characters before calling SetValue on ES_NUMBER fields
- Use char.IsLetter checks in your own pre-validation to reject bad input early
- If the field must accept non-numeric text, change the control style (remove ES_NUMBER) in the target app
- Parse the numeric portion (e.g. double.TryParse) and set only the numeric string
Example fix
// before
valuePattern.SetValue("12 kg"); // ArgumentException: NotAValidValue
// after
string numeric = new string(text.Where(c => !char.IsLetter(c)).ToArray());
if (!string.IsNullOrWhiteSpace(numeric))
valuePattern.SetValue(numeric); Defensive patterns
Strategy: validation
Validate before calling
bool IsNumericSafeForEsNumber(string s) => !s.Any(char.IsLetter);
Type guard
bool IsNumericText(string s) => s.All(c => !char.IsLetter(c));
Try / catch
try { valuePattern.SetValue(val); } catch (ArgumentException) { /* value contains letters on an ES_NUMBER field */ } Prevention
- Sanitize input to digits/separators before writing to numeric-only fields
- Detect ES_NUMBER-style fields (control type Edit with numeric semantics) and validate first
- Use invariant formatting when converting numbers to strings for automation
When it happens
Trigger: Calling ValuePattern.SetValue(str) on an ES_NUMBER-styled rich edit with a string containing any letter (e.g. "12a4").
Common situations: Automation feeding unvalidated user input or formatted strings ("12 kg", "1.5x") into numeric-only fields; locale-specific digit/letter characters sneaking into values.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- ' ' is not a valid value for this control.
- SR.AtLeastOnePropertyMustBeSpecified
- SR.Format(SR.RichEditTextPatternHasNoChildren…
- SR.InvalidParameter
- SR.NoITextDocumentFromRichEdit
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/43f283dccec1a0d8.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsRichEdit.cs:180
if (!SafeNativeMethods.IsWindowEnabled(_hwnd))
{
throw new ElementNotEnabledException();
}
if (Misc.IsBitSet(WindowStyle, NativeMethods.ES_READONLY))
{
throw new InvalidOperationException(SR.ValueReadonly);
}
// check if control only accepts numbers
if (Misc.IsBitSet(WindowStyle, NativeMethods.ES_NUMBER) && !WindowsFormsHelper.IsWindowsFormsControl(_hwnd))
{
// check if string contains any non-numeric characters.
foreach (char ch in str)
{
if (char.IsLetter(ch))
{
throw new ArgumentException(SR.Format(SR.NotAValidValue, str), "val");
}
}
}
// Text/edit box should not enter more characters than what is allowed through keyboard.
// Determine the max number of chars this editbox accepts
int result = Misc.ProxySendMessageInt(_hwnd, NativeMethods.EM_GETLIMITTEXT, IntPtr.Zero, IntPtr.Zero);
if (result < str.Length)
{
throw new InvalidOperationException (SR.OperationCannotBePerformed);
}
// Send the message...
result = Misc.ProxySendMessageInt(_hwnd, NativeMethods.WM_SETTEXT, IntPtr.Zero, new StringBuilder(str));
if (result != 1)
{
throw new InvalidOperationException(SR.OperationCannotBePerformed);View on GitHub (pinned to 81131a70a4)