Control.IsMnemonic(Char, String) 方法

定義

判斷指定的字元是否為在指定字串中指派的助憶鍵字元。

C#
public static bool IsMnemonic(char charCode, string text);
C#
public static bool IsMnemonic(char charCode, string? text);

參數

charCode
Char

要測試的字元。

text
String

要搜尋的字串。

傳回

如果 charCode 字元是指派至控制項的助憶鍵字元,是 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;
    }
}

備註

助記字元是 緊接在 中 String「&」 實例後面的字元。

適用於

產品 版本
.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

另請參閱