Skip to content

Runtime crash due to localization and the way designer serializes keyboard shortcuts #2886

@filipnavara

Description

@filipnavara
  • .NET Core Version: 3.1.2

  • Have you experienced this same bug with .NET Framework?: No

Problem description:

Steps to reproduce:

  • Create a blank .NET Framework WinForms application with a Form.
  • Set the Form to Localizable = true in the designer.
  • Add context menu with single item.
  • Set the ShortcutKeys on the item to Control+C.
  • Observe that the application works just fine on .NET Framework 4.8.
  • Switch locale at startup to German by System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-DE");
  • Observe that the application still starts just fine on .NET Framework 4.8.
  • Change the application to target netcoreapp31 and it immediately crashes on startup.

The reason for the crash is that the .resx file contains the shortcut key serialized as

  <data name="booToolStripMenuItem.ShortcutKeys" type="System.Windows.Forms.Keys, System.Windows.Forms">
    <value>Ctrl+C</value>
  </data>

Notice that the serialized value starts with Ctrl, not Control. It is produced and consumed by KeysConverter class that is locale dependent. While the value Control would work in any locale due to the fallback to names in the Keys enum the value Ctrl is taken from a dictionary that contains localized names. On .NET Core 3.1 in the German localization the name for Control key is Strg. Thus the value Ctrl+C in the resource file is invalid and causes conversion error at runtime.

Notably we migrated a large project from .NET Framework to .NET Core where we hit this issue. The .NET Framework also has similar issue in code but the localizations may differ which would explain why the problem is not hit.

Manually fixing the value in the .resx file to Control+C makes the application work in any locale but any further editing in the designer breaks it again.

Expected behavior:

No crash. The resources generated by the designer should be locale independent.

Minimal repro:

Here's the sample app created using the steps above. It is multi-targeting both .NET Framework and .NET Core so it can run in both variation from inside Visual Studio.

WindowsFormsApp2.zip

Metadata

Metadata

Labels

🪲 bugProduct bug (most likely)area-Serialization-Resxarea-VSDesignerWindows Forms out-of-proc designer related issuestenet-localizationSuggested translations of resources; notification requiring translations; internationalization

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions