HandyOrg/HandyControl · error · InvalidEnumArgumentException

defaultResult

Error message

defaultResult

What it means

Validation helper in MessageBox.CreateMessageBox: throws InvalidEnumArgumentException for 'defaultResult' when the MessageBoxResult default value is not a defined member (None, OK, Cancel, Yes, No). It fires when a caller passes an undefined default result, or one inconsistent with the button set.

Solutions

  1. Only pass defined MessageBoxResult values that are compatible with the chosen MessageBoxButton combination
  2. Validate with Enum.IsDefined(typeof(MessageBoxResult), defaultResult) before calling Show
  3. Guard public wrappers so defaultResult defaults to OK when unspecified
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/Shared/HandyControl_Shared/Controls/Window/MessageBox.cs:492 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of HandyOrg/HandyControl@2c0875ebd6 (2026-09-14). Data as JSON: /api/errors/2cc8cdf2a1c67444. Report an issue: GitHub.

Appendix: source

Thrown at src/Shared/HandyControl_Shared/Controls/Window/MessageBox.cs:492

        string caption,
        MessageBoxButton button,
        MessageBoxImage icon,
        MessageBoxResult defaultResult
    )
    {
        if (!IsValidMessageBoxButton(button))
        {
            throw new InvalidEnumArgumentException(nameof(button), (int) button, typeof(MessageBoxButton));
        }

        if (!IsValidMessageBoxImage(icon))
        {
            throw new InvalidEnumArgumentException(nameof(icon), (int) icon, typeof(MessageBoxImage));
        }

        if (!IsValidMessageBoxResult(defaultResult))
        {
            throw new InvalidEnumArgumentException(nameof(defaultResult), (int) defaultResult,
                typeof(MessageBoxResult));
        }

        var ownerWindow = owner ?? WindowHelper.GetActiveWindow();
        var ownerIsNull = ownerWindow is null;

        return new MessageBox
        {
            Message = messageBoxText,
            Owner = ownerWindow,
            WindowStartupLocation =
                ownerIsNull ? WindowStartupLocation.CenterScreen : WindowStartupLocation.CenterOwner,
            ShowTitle = true,
            Title = caption ?? string.Empty,
            Topmost = ownerIsNull,
            _messageBoxResult = defaultResult
        };
    }

View on GitHub (pinned to 2c0875ebd6)