dotnet/wpf · error · InvalidOperationException
Operation cannot be performed.
Error message
Operation cannot be performed.
What it means
The Win32 edit box proxy throws InvalidOperationException(OperationCannotBePerformed) from IValueProvider.SetValue when the requested text is longer than the control's text limit. It queries EM_GETLIMITTEXT; if a limit is set (result != -1) and the string exceeds it, the operation is rejected before sending WM_SETTEXT.
Solutions
- Truncate the string to the control's limit before calling SetValue.
- Raise the limit in the target application via EM_SETLIMITTEXT.
- Use a multiline/large control (or write to the data source directly) for long content.
- Catch InvalidOperationException and split input into allowed-size chunks if the control semantics permit.
Example fix
// before valuePattern.SetValue(longText); // throws if longText.Length > limit // after int limit = /* EM_GETLIMITTEXT result, or int.MaxValue if -1 */; valuePattern.SetValue(longText.Length > limit ? longText.Substring(0, limit) : longText);
Defensive patterns
Strategy: validation
Validate before calling
int limit = SendMessage(hwnd, EM_GETLIMITTEXT, 0, 0);
if (limit != -1 && text.Length > limit)
text = text.Substring(0, limit); // or reject Try / catch
try { vp.SetValue(text); }
catch (InvalidOperationException) { /* text exceeds EM limit; truncate or split */ } Prevention
- Query EM_GETLIMITTEXT before writing.
- Keep generated test data within control limits.
- Raise EM_SETLIMITTEXT in the app when longer content is required.
When it happens
Trigger: Calling IValueProvider.SetValue with a string whose length exceeds the value returned by EM_GETLIMITTEXT for the edit control (limit set via EM_SETLIMITTEXT or the control default, e.g. 32766 chars).
Common situations: Pasting large log text or multiline blobs into a short input field; a test framework filling a max-length-limited field with generated data; migrating scripts between controls with different limits.
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
- ' ' is not a valid value for this control.
- Value is read-only.
- Element is not enabled.
- Element is not enabled.
- Element is not enabled.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/26118d988ec5ee35.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClientSideProviders/MS/Internal/AutomationProxies/WindowsEditBox.cs:293
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);
}
}
// Request to get the value that this UI element is representing as a string
string IValueProvider.Value
{
get
{
return GetValue();
}
}View on GitHub (pinned to 81131a70a4)