Control.ProcessMnemonic(Char) 方法

定义

处理助记键字符。

C#
protected virtual bool ProcessMnemonic(char charCode);
C#
protected internal virtual bool ProcessMnemonic(char charCode);

参数

charCode
Char

要处理的字符。

返回

如果字符由控件作为助记键处理,则为 true;否则为 false

示例

下面的代码示例演示了按钮类的扩展,该扩展替代 ProcessMnemonic 方法以展示自定义行为。 该示例还演示了 和 IsMnemonic 属性的CanSelect用法。 若要运行此示例,请将以下代码粘贴到同一文件中的窗体类后面。 向窗体添加类型的 MnemonicButton 按钮。

C#
// This button is a simple extension of the button class that overrides
// the ProcessMnemonic method.  If the mnemonic is correctly entered,  
// the message box will appear and the click event will be raised.  
public class MyMnemonicButton : Button
{
    // This method makes sure the control is selectable and the 
    // mneumonic is correct before displaying the message box
    // and triggering the click event.
    protected override bool ProcessMnemonic(char inputChar)
    {
        if (CanSelect && IsMnemonic(inputChar, this.Text))
        {
            MessageBox.Show("You've raised the click event " +
                "using the mnemonic.");
            this.PerformClick();
            return true;
        }
        return false;
    }
}

注解

调用此方法可让控件有机会处理助记符。 方法应确定控件是否处于处理助记符的状态,以及给定字符是否表示助记符。 如果是这样,方法应执行与助记符关联的操作并返回 true。 否则,方法应返回 false。 此方法的实现通常使用 IsMnemonic 方法来确定给定字符是否与控件文本中的助记符匹配。

例如:

C#
if (CanSelect && IsMnemonic(charCode, MyControl.Text) {
      // Perform action associated with mnemonic.
       }

方法的 ProcessMnemonic 此默认实现只是为了 false 指示控件没有助记符。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅