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

こちらもご覧ください