この記事は翻訳者によって翻訳されたものです。 記事の文章にポインターを重ねると、原文のテキストが表示されます。
訳文
原文
このトピックはまだ評価されていません - このトピックを評価する

Control.KeyDown イベント

コントロールにフォーカスがあるときにキーが押されると発生します。

名前空間:  System.Windows.Forms
アセンブリ:  System.Windows.Forms (System.Windows.Forms.dll 内)
public event KeyEventHandler KeyDown

キー イベントは次の順序で発生します。

  1. KeyDown

  2. KeyPress

  3. KeyUp

キーボード イベントをフォームでだけ処理し、そのイベントを他のコントロールでは受け取らないようにする場合は、フォームの KeyPress イベント処理メソッドの KeyPressEventArgs.Handled プロパティを true に設定します。 Tab キー、Return キー、Esc キー、方向キーなどは、コントロールによって自動処理されます。 これらのキーで KeyDown イベントを発生させるには、フォーム上の各コントロールで IsInputKey メソッドをオーバーライドする必要があります。 IsInputKey のオーバーライドのコードは、特殊なキーのいずれかが押され、そのキーが true の値を返したかどうかを判断する必要があります。 IsInputKey メソッドをオーバーライドする代わりに、PreviewKeyDown イベントを処理し、IsInputKey プロパティを true に設定できます。 コード例については、PreviewKeyDown イベントのトピックを参照してください。

イベント処理の詳細については、「イベントの利用」を参照してください。

KeyDown イベントを使用して、コントロールに入力された文字の種類を判断するコード例を次に示します。


        // Boolean flag used to determine when a character other than a number is entered.
        private bool nonNumberEntered = false;

        // Handle the KeyDown event to determine the type of character entered into the control.
        private void textBox1_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            // Initialize the flag to false.
            nonNumberEntered = false;

            // Determine whether the keystroke is a number from the top of the keyboard.
            if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
            {
                // Determine whether the keystroke is a number from the keypad.
                if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
                {
                    // Determine whether the keystroke is a backspace.
                    if(e.KeyCode != Keys.Back)
                    {
                        // A non-numerical keystroke was pressed.
                        // Set the flag to true and evaluate in KeyPress event.
                        nonNumberEntered = true;
                    }
                }
            }
            //If shift key was pressed, it's not a number.
            if (Control.ModifierKeys == Keys.Shift) {
                nonNumberEntered = true;
            }
        }

        // This event occurs after the KeyDown event and can be used to prevent
        // characters from entering the control.
        private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            // Check for the flag being set in the KeyDown event.
            if (nonNumberEntered == true)
            {
                // Stop the character from being entered into the control since it is non-numerical.
                e.Handled = true;
            }
        }



.NET Framework

サポート対象: 4、3.5、3.0、2.0、1.1、1.0

.NET Framework Client Profile

サポート対象: 4、3.5 SP1

Windows 7, Windows Vista SP1 以降, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2

.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
この情報は役に立ちましたか。
(残り 1500 文字)
コミュニティ コンテンツ 追加
注釈 FAQ