dotnet/wpf · error · ArgumentException

SR.InputMethod_InvalidConversionMode (value)

Error message

SR.InputMethod_InvalidConversionMode (value)

What it means

InputMethod.ConversionMode setter throws ArgumentException wrapping SR.InputMethod_InvalidConversionMode when the value fails IsValidConversionMode, i.e. it is not a valid combination of ImeConversionModeValues flags (Native, Katakana, FullShape, Roman, CharPrecision, etc.). Invalid flag combos or out-of-range bits are rejected before the mode is pushed to the IME/Cicero.

Solutions

  1. Only assign values built from ImeConversionModeValues enum members (bitwise OR of valid flags).
  2. Mask or validate the stored value before applying it: if ((v & ~ImeConversionModeValues.Alphanumeric) != 0) fall back to a default.
  3. Wrap the setter in try-catch for ArgumentException and reset to ImeConversionModeValues.DoNotCare on failure when loading legacy settings.

Example fix

// before
ime.ConversionMode = (ImeConversionModeValues)storedInt; // may be invalid
// after
var v = (ImeConversionModeValues)storedInt;
ime.ConversionMode = (v & ImeConversionModeValues.DoNotCare) != 0 || (v & ~validMask) != 0
    ? ImeConversionModeValues.DoNotCare : v;
Defensive patterns

Strategy: validation

Validate before calling

static readonly ImeConversionModeValues ValidMask =
    ImeConversionModeValues.Alphanumeric | ImeConversionModeValues.Native |
    ImeConversionModeValues.Katakana | ImeConversionModeValues.FullShape |
    ImeConversionModeValues.Roman | ImeConversionModeValues.CharPrecision |
    ImeConversionModeValues.Symbol | ImeConversionModeValues.Fixed;
if (((int)value & ~(int)ValidMask) != 0) value = ImeConversionModeValues.DoNotCare;

Type guard

static bool IsValidConversionModeValue(ImeConversionModeValues v) =>
    (v & ~(ImeConversionModeValues)0x1FFFF) == 0;

Try / catch

try { ime.ConversionMode = v; }
catch (ArgumentException) { ime.ConversionMode = ImeConversionModeValues.DoNotCare; }

Prevention

When it happens

Trigger: Assigning a value to InputMethod.ConversionMode that is not one of the defined ImeConversionModeValues combinations, e.g. an undefined bit, negative value, or a combination including DoNotCare mixed with other flags.

Common situations: Persisting/restoring IME modes from config files or the registry where stale or hand-edited numeric values appear; computing modes with bitwise OR of unintended constants; localization code for Japanese/Chinese/Korean IMEs.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputMethod.cs:811

                        if ((convmode & NativeMethods.IME_CMODE_EUDC) != 0)
                            ret |= ImeConversionModeValues.Eudc;
                        if ((convmode & NativeMethods.IME_CMODE_SYMBOL) != 0)
                            ret |= ImeConversionModeValues.Symbol;
                        if ((convmode & NativeMethods.IME_CMODE_FIXED) != 0)
                            ret |= ImeConversionModeValues.Fixed;

                        return ret;
                    }
                }

                return ImeConversionModeValues.Alphanumeric;
            }

            set
            {
                if (!IsValidConversionMode(value))
                {
                    throw new ArgumentException(SR.Format(SR.InputMethod_InvalidConversionMode, value));
                }

                Debug.Assert((value & ImeConversionModeValues.DoNotCare) == 0);

                IntPtr hwnd = IntPtr.Zero;
                if (_immEnabled)
                {
                    hwnd = HwndFromInputElement(Keyboard.FocusedElement);
                }


                //
                // Update Cicero's conversion mode.
                //
                TextServicesCompartment compartment;
                compartment = TextServicesCompartmentContext.Current.GetCompartment(InputMethodStateType.ImeConversionModeValues);

                if (compartment != null)

View on GitHub (pinned to 81131a70a4)