Control.IsMnemonic(Char, String) 方法

定义

确定指定字符是否为指定字符串中分配给控件的助记字符。

public:
 static bool IsMnemonic(char charCode, System::String ^ text);
public static bool IsMnemonic(char charCode, string text);
public static bool IsMnemonic(char charCode, string? text);
static member IsMnemonic : char * string -> bool
Public Shared Function IsMnemonic (charCode As Char, text As String) As Boolean

参数

charCode
Char

要测试的字符。

text
String

要搜索的字符串。

返回

true charCode如果字符是分配给控件的助记字符,则为 ;否则为 false

示例

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

// 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.  
// This method makes sure the control is selectable and the 
// mnemonic is correct before displaying the message box
// and triggering the click event.
public ref class MyMnemonicButton: public Button
{
protected:
   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;
   }

};
// 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;
    }
}
' 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
    Inherits Button

    ' This method makes sure the control is selectable and the 
    ' mneumonic is correct before displaying the message box
    ' and triggering the click event.
    <System.Security.Permissions.UIPermission( _
    System.Security.Permissions.SecurityAction.Demand, Window:=UIPermissionWindow.AllWindows)> _
    Protected Overrides Function ProcessMnemonic( _
        ByVal inputChar As Char) As Boolean

        If (CanSelect And IsMnemonic(inputChar, Me.Text)) Then
            MessageBox.Show("You've raised the click event " _
                & "using the mnemonic.")
            Me.PerformClick()
            Return True
        End If
        Return False
    End Function

End Class

注解

助记字符是紧接在 “&&” 的第一个 String实例后面的字符。

适用于

另请参阅