CA1301: Avoid duplicate accelerators


For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

Breaking ChangeNon-breaking

A type extends System.Windows.Forms.Control and contains two or more top level controls that have identical access keys that are stored in a resource file.

An access key, also known as an accelerator, enables keyboard access to a control by using the ALT key. When multiple controls have duplicate access keys, the behavior of the access key is not well defined. The user might not be able to access the intended control by using the access key and a control other than the one that is intended might be enabled.

The current implementation of this rule ignores menu items. However, menu items in the same submenu should not have identical access keys.

To fix a violation of this rule, define unique access keys for all controls.

Do not suppress a warning from this rule.

The following example shows a minimal form that contains two controls that have identical access keys. The keys are stored in a resource file, which is not shown; however, their values appear in the commented out checkBox.Text lines. The behavior of duplicate accelerators can be examined by exchanging the checkBox.Text lines with their commented out counterparts. However, in this case, the example will not generate a warning from the rule.

using System;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;

namespace GlobalizationLibrary
   public class DuplicateAccelerators : Form
      public static void Main()
         DuplicateAccelerators accelerators = new DuplicateAccelerators();

      private CheckBox checkBox1;
      private CheckBox checkBox2;

      public DuplicateAccelerators()
         ResourceManager resources = 
            new ResourceManager(typeof(DuplicateAccelerators));

         checkBox1 = new CheckBox();
         checkBox1.Location = new Point(8, 16);
         // checkBox1.Text = "&checkBox1";
         checkBox1.Text = resources.GetString("checkBox1.Text");

         checkBox2 = new CheckBox();
         checkBox2.Location = new Point(8, 56);
         // checkBox2.Text = "&checkBox2";
         checkBox2.Text = resources.GetString("checkBox2.Text");


Resources in Desktop Apps