Form.KeyPreview プロパティとは何? わかりやすく解説 Weblio辞書

Form.KeyPreview プロパティとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Form.KeyPreview プロパティの意味・解説 

Form.KeyPreview プロパティ

キー イベントフォーカスを持つコントロール渡される前にフォームがそのイベント受け取かどうかを示す値を取得または設定します

名前空間: System.Windows.Forms
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文構文

解説解説

このプロパティtrue設定すると、KeyPress、KeyDown、KeyUp の各イベントをすべてフォーム受け取りますフォームイベントハンドラキーストロークの処理が完了してから、フォーカスを持つコントロールにそのキーストローク割り当てられます。たとえば、KeyPreview プロパティtrue設定され、現在選択されているコントロールTextBox場合は、キーストロークフォームイベント ハンドラ処理され後で押されキーTextBox コントロール受け取りますキーボード イベントフォームでだけ処理し、そのイベントコントロールでは受け取らないようにする場合は、フォームKeyPress イベント ハンドラの KeyPressEventArgs.Handled プロパティtrue設定します

このプロパティ使用してアプリケーションのほとんどのキーストローク処理しフォームキーストローク処理するか、キーストローク処理するために適切なコントロール呼び出すことができます。たとえば、アプリケーションファンクション キー使用される場合は、キーストローク イベント受け取コントロールごとにコード作成するではなくフォーム レベルキーストローク処理します

メモメモ

フォーム表示できるコントロールまたは有効なコントロールない場合は、すべてのキーボード イベントフォーム自動的に受け取ります

使用例使用例

フォームKeyPreview プロパティtrue設定してフォーム レベルキー イベント処理するコード例次に示します。この例を実行するには、次のコードを空のフォーム貼り付けます。

using namespace System::Windows::Forms;

// 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;
   }

};


// Declare the controls contained on the form.
public ref class Form1: public
 System::Windows::Forms::Form
{
private:
   MyMnemonicButton^ button1;

public private:
   System::Windows::Forms::ListBox^ ListBox1;

public:
   Form1()
      : Form()
   {
      
      // Set KeyPreview object to true to allow the form to process
 
      // the key before the control with focus processes it.
      this->KeyPreview = true;
      
      // Add a MyMnemonicButton.  
      button1 = gcnew MyMnemonicButton;
      button1->Text = "&Click";
      button1->Location = System::Drawing::Point( 100, 120 );
      this->Controls->Add( button1 );
      
      // Initialize a ListBox control and the form itself.
      this->ListBox1 = gcnew System::Windows::Forms::ListBox;
      this->SuspendLayout();
      this->ListBox1->Location = System::Drawing::Point(
 8, 8 );
      this->ListBox1->Name = "ListBox1";
      this->ListBox1->Size = System::Drawing::Size( 120,
 95 );
      this->ListBox1->TabIndex = 0;
      this->ListBox1->Text = "Press a key";
      this->ClientSize = System::Drawing::Size( 292, 266 );
      this->Controls->Add( this->ListBox1
 );
      this->Name = "Form1";
      this->Text = "Form1";
      this->ResumeLayout( false );
      
      // Associate the event-handling method with the
      // KeyDown event.
      this->KeyDown += gcnew KeyEventHandler( this,
 &Form1::Form1_KeyDown );
   }


private:

   // The form will handle all key events before the control with  
   // focus handles them.  Show the keys pressed by adding the
   // KeyCode object to ListBox1. Ensure the processing is passed
   // to the control with focus by setting the KeyEventArg.Handled
   // property to false.
   void Form1_KeyDown( Object^ /*sender*/, KeyEventArgs^ e )
   {
      ListBox1->Items->Add( e->KeyCode );
      e->Handled = false;
   }

};


[System::STAThreadAttribute]
int main()
{
   Application::Run( gcnew Form1 );
}

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

Form.KeyPreview プロパティのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



Form.KeyPreview プロパティのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2024 Microsoft.All rights reserved.

©2024 GRAS Group, Inc.RSS