dotnet/wpf · error · ArgumentException
' ' is not a valid value for this control.
Error message
'{0}' is not a valid value for this control. What it means
The Win32 edit box proxy throws ArgumentException(NotAValidValue) from IValueProvider.SetValue when the control has the ES_NUMBER style but the supplied string contains alphabetic characters. A numeric-only edit control cannot accept non-numeric text.
Solutions
- Strip or validate the string so it contains only numeric characters before calling SetValue.
- Pass the number formatted as digits only, e.g. value.ToString(CultureInfo.InvariantCulture).
- If non-numeric text is genuinely needed, recreate the edit control without ES_NUMBER.
- Catch ArgumentException and surface a clear 'numeric field' validation message to the user.
Example fix
// before
valuePattern.SetValue("12 items"); // ArgumentException: not a valid value
// after
string numeric = new string(raw.Where(char.IsDigit).ToArray());
if (numeric.Length > 0) valuePattern.SetValue(numeric); Defensive patterns
Strategy: validation
Validate before calling
if (raw.Any(char.IsLetter))
throw new ArgumentException("Numeric field requires digits only");
valuePattern.SetValue(raw); Try / catch
try { vp.SetValue(s); }
catch (ArgumentException) { /* reject non-numeric input upstream */ } Prevention
- Sanitize input to digits for ES_NUMBER controls.
- Use InvariantCulture number formatting.
- Validate at the data-entry boundary, not the UI.
When it happens
Trigger: Calling IValueProvider.SetValue on a WindowsEditBox whose window styles include ES_NUMBER, with a string containing any letter (char.IsLetter).
Common situations: Automating numeric input fields (age, quantity, port) and passing formatted values like '12abc', 'N/A', or values with letters from a spreadsheet; localized number formatting carrying letter suffixes.
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
- Operation cannot be performed.
- throw new…
- Value is read-only.
- Buffer size is too small to accommodate the specified…
- Cannot have leading path delimiter.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/a40b49b4cd190d33.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBox.cs:282
// Ensure that the edit box and all its parents are enabled.
Misc.CheckEnabled(_hwnd);
int styles = WindowStyle;
if (Misc.IsBitSet(styles, NativeMethods.ES_READONLY))
{
throw new InvalidOperationException(SR.ValueReadonly);
}
// check if control only accepts numbers
if (Misc.IsBitSet(styles, NativeMethods.ES_NUMBER))
{
// 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);
// A result of -1 means that no limit is set.
if (result != -1 && 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)