ContextMenu イベント

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。 ( Component から継承されます。) |
![]() | Popup | ショートカット メニューが表示される前に発生します。 |

ContextMenu クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


ContextMenu クラスは、ユーザーがコントロールまたはフォームの領域の上で、右クリックすると表示されるショートカット メニューを表します。通常、ショートカット メニューはフォームの MainMenu のさまざまなメニュー項目を組み合わせるために使用されます。ショートカット メニューは、アプリケーションのコンテキストが指定されるためユーザーにとっては使いやすくなります。たとえば、TextBox に割り当てられたショートカット メニューを使用すると、テキストのフォントを変更したり、コントロール内のテキストを検索するためのメニュー項目、またはテキストをコピーして貼り付けるクリップボード機能を使用できます。また、MainMenu 内にない新しい MenuItem オブジェクトをショートカット メニューに表示して、MainMenu では表示されない、状況に合ったコマンドを使用することもできます。
通常、ショートカット メニューは、ユーザーがコントロールまたはフォーム自体の上で右ボタンをクリックすると表示されます。表示されているコントロールおよび Form は、ショートカット メニューを表示するコントロールに ContextMenu クラスをバインドする ContextMenu プロパティを持っています。ContextMenu は複数のコントロールで使用できます。SourceControl プロパティを使用すると、コントロールに固有のタスクの実行、またはコントロールに表示されるショートカット メニューの変更のために、どのコントロールで最後にショートカット メニューが表示されたかを確認できます。
メニューが表示される前に、チェック マークの設定、項目の無効化、および他のメニュー タスクの実行のために、いつショートカット メニューを表示していたかを知ることができます。Popup イベントを処理すると、いつショートカット メニューが表示されていたかを確認できます。
![]() |
---|
MainMenu で表示されている MenuItem オブジェクトを再利用して、ContextMenu で使用するには、MenuItem クラスの CloneMenu メソッドを使用してメニューのコピーを作成する必要があります。MenuItem クラスの MergeMenu メソッドを使用すると、メニュー項目とそのサブメニュー項目を 1 つの MenuItem オブジェクトにマージすることもできます。 |
Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows CE プラットフォームメモ : Pocket PC では、子フォームでのショートカット メニューの使用を避けてください。その親フォームを破棄した後でも表示されたままとなるためです。

ContextMenu の Popup イベントのイベント ハンドラを作成するコード例を次に示します。このイベント ハンドラのコードは、ショートカット メニューを表示しているコントロールが pictureBox1 という名前の PictureBox コントロールと textBox1 という名前の TextBox コントロールのどちらであるかを判断します。どちらのコントロールが ContextMenu のショートカット メニューを表示したかに応じて、適切な MenuItem オブジェクトが ContextMenu に追加されます。この例では、フォーム内で定義されている contextMenu1 という名前の ContextMenu クラスのインスタンスが既に存在する必要があります。またこの例では、TextBox と PictureBox がフォームに追加されており、これらのコントロールの ContextMenu プロパティが contextMenu1 に設定されている必要があります。
Private Sub MyPopupEventHandler(sender As System.Object, e As System.EventArgs) ' Define the MenuItem objects to display for the TextBox. Dim menuItem1 As New MenuItem("&Copy") Dim menuItem2 As New MenuItem("&Find and Replace") ' Define the MenuItem object to display for the PictureBox. Dim menuItem3 As New MenuItem("C&hange Picture") ' Clear all previously added MenuItems. contextMenu1.MenuItems.Clear() If contextMenu1.SourceControl Is textBox1 Then ' Add MenuItems to display for the TextBox. contextMenu1.MenuItems.Add(menuItem1) contextMenu1.MenuItems.Add(menuItem2) ElseIf contextMenu1.SourceControl Is pictureBox1 Then ' Add the MenuItem to display for the PictureBox. contextMenu1.MenuItems.Add(menuItem3) End If End Sub 'MyPopupEventHandler '
private void MyPopupEventHandler(System.Object sender, System.EventArgs e) { // Define the MenuItem objects to display for the TextBox. MenuItem menuItem1 = new MenuItem("&Copy"); MenuItem menuItem2 = new MenuItem("&Find and Replace"); // Define the MenuItem object to display for the PictureBox. MenuItem menuItem3 = new MenuItem("C&hange Picture"); // Clear all previously added MenuItems. contextMenu1.MenuItems.Clear(); if(contextMenu1.SourceControl == textBox1) { // Add MenuItems to display for the TextBox. contextMenu1.MenuItems.Add(menuItem1); contextMenu1.MenuItems.Add(menuItem2); } else if(contextMenu1.SourceControl == pictureBox1) { // Add the MenuItem to display for the PictureBox. contextMenu1.MenuItems.Add(menuItem3); } }
private: void MyPopupEventHandler( System::Object^ /*sender*/, System::EventArgs^ /*e*/ ) { // Define the MenuItem objects to display for the TextBox. MenuItem^ menuItem1 = gcnew MenuItem( "&Copy" ); MenuItem^ menuItem2 = gcnew MenuItem( "&Find and Replace" ); // Define the MenuItem object to display for the PictureBox. MenuItem^ menuItem3 = gcnew MenuItem( "C&hange Picture" ); // Clear all previously added MenuItems. contextMenu1->MenuItems->Clear(); if ( contextMenu1->SourceControl == textBox1 ) { // Add MenuItems to display for the TextBox. contextMenu1->MenuItems->Add( menuItem1 ); contextMenu1->MenuItems->Add( menuItem2 ); } else if ( contextMenu1->SourceControl == pictureBox1 ) { // Add the MenuItem to display for the PictureBox. contextMenu1->MenuItems->Add( menuItem3 ); } }
private void MyPopupEventHandler(Object sender, EventArgs e) { // Define the MenuItem objects to display for the TextBox. MenuItem menuItem1 = new MenuItem("&Copy"); MenuItem menuItem2 = new MenuItem("&Find and Replace"); // Define the MenuItem object to display for the PictureBox. MenuItem menuItem3 = new MenuItem("C&hange Picture"); // Clear all previously added MenuItems. contextMenu1.get_MenuItems().Clear(); if (contextMenu1.get_SourceControl().Equals(textBox1)) { // Add MenuItems to display for the TextBox. contextMenu1.get_MenuItems().Add(menuItem1); contextMenu1.get_MenuItems().Add(menuItem2); } else { if (contextMenu1.get_SourceControl().Equals(pictureBox1)) { // Add the MenuItem to display for the PictureBox. contextMenu1.get_MenuItems().Add(menuItem3); } } } //MyPopupEventHandler

System.MarshalByRefObject
System.ComponentModel.Component
System.Windows.Forms.Menu
System.Windows.Forms.ContextMenu


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ContextMenu コンストラクタ ()
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


このバージョンのコンストラクタを使用した後で、Menu.MenuItemCollection クラスの Add メソッドを使用して、ContextMenu にメニュー項目を追加できます。Menu.MenuItemCollection にアクセスするには、MenuItems プロパティを使用します。

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ContextMenu コンストラクタ (MenuItem[])
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)


このバージョンのコンストラクタを使用すると、指定したメニュー項目を作成時に保持している ContextMenu を作成できます。このバージョンのコンストラクタを使用した後で、Menu.MenuItemCollection クラスの Add メソッドを使用して、ContextMenu にメニュー項目を追加できます。Menu.MenuItemCollection にアクセスするには、MenuItems プロパティを使用します。

ショートカット メニューを構築し、Show メソッドを使用する方法を、次のコード例に示します。この例を実行するには、次のコードを、Button1 という名前のボタンが配置されているフォームに貼り付けます。必ずすべてのイベントをイベント処理メソッドに関連付けるようにしてください。
' Displays the shortcut menu, offsetting its location ' from the upper-left corner of Button1 by 20 pixels in each direction. Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click 'Declare the menu items and the shortcut menu. Dim menuItems() As MenuItem = New MenuItem() _ {New MenuItem("Some Button Info"), _ New MenuItem("Some Other Button Info"), _ New MenuItem("Exit")} Dim buttonMenu As New ContextMenu(menuItems) buttonMenu.Show(Button1, New System.Drawing.Point(20, 20)) End Sub
// Displays the shortcut menu, offsetting its location // from the upper-left corner of Button1 by 20 pixels in each direction. private void Button1_Click(System.Object sender, System.EventArgs e) { //Declare the menu items and the shortcut menu. MenuItem[] menuItems = new MenuItem[]{new MenuItem("Some Button Info"), new MenuItem("Some Other Button Info"), new MenuItem("Exit")}; ContextMenu buttonMenu = new ContextMenu(menuItems); buttonMenu.Show(Button1, new System.Drawing.Point(20, 20)); }
// Displays the shortcut menu, offsetting its location // from the upper-left corner of Button1 by 20 pixels in each direction. void Button1_Click( System::Object^ /*sender*/, System::EventArgs^ /*e*/ ) { //Declare the menu items and the shortcut menu. array<MenuItem^>^menuItems = {gcnew MenuItem( "Some Button Info" ),gcnew MenuItem( "Some Other Button Info" ),gcnew MenuItem( "Exit" )}; System::Windows::Forms::ContextMenu^ buttonMenu = gcnew System::Windows::Forms::ContextMenu( menuItems ); buttonMenu->Show( Button1, System::Drawing::Point( 20, 20 ) ); }
// Displays the shortcut menu, offsetting its location // from the upper-left corner of Button1 by 20 pixels in each direction. private void button1_Click(Object sender, System.EventArgs e) { //Declare the menu items and the shortcut menu. MenuItem menuItems[] = new MenuItem[] { new MenuItem("Some Button Info"), new MenuItem("Some Other Button Info"), new MenuItem("Exit") }; ContextMenu buttonMenu = new ContextMenu(menuItems); buttonMenu.Show(button1, new System.Drawing.Point(20, 20)); } //button1_Click

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


ContextMenu コンストラクタ

名前 | 説明 |
---|---|
ContextMenu () | メニュー項目を指定せずに、ContextMenu クラスの新しいインスタンスを初期化します。 .NET Compact Framework によってサポートされています。 |
ContextMenu (MenuItem[]) | 一連の MenuItem オブジェクトを指定して、ContextMenu クラスの新しいインスタンスを初期化します。 |

ContextMenu プロパティ

名前 | 説明 | |
---|---|---|
![]() | Container | Component を格納している IContainer を取得します。 ( Component から継承されます。) |
![]() | Handle | メニューのウィンドウ ハンドルを表している値を取得します。 ( Menu から継承されます。) |
![]() | IsParent | このメニューにメニュー項目が格納されているかどうかを示す値を取得します。このプロパティは読み取り専用です。 ( Menu から継承されます。) |
![]() | MdiListItem | マルチ ドキュメント インターフェイス (MDI) 子フォームの一覧を表示するために使用される MenuItem を示す値を取得します。 ( Menu から継承されます。) |
![]() | MenuItems | メニューに関連付けられている MenuItem オブジェクトのコレクションを示す値を取得します。 ( Menu から継承されます。) |
![]() | Name | Menu の名前を取得または設定します。 ( Menu から継承されます。) |
![]() | Site | Component の ISite を取得または設定します。 ( Component から継承されます。) |
![]() ![]() | Tag | コントロールに関連付けられたユーザー定義のデータを取得または設定します。 ( Menu から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。 ( Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。 ( Component から継承されます。) |

ContextMenu メソッド


名前 | 説明 | |
---|---|---|
![]() | CloneMenu | 現在の Menu にパラメータとして渡された Menu をコピーします。 ( Menu から継承されます。) |
![]() | CreateMenuHandle | Menu への新しいハンドルを作成します。 ( Menu から継承されます。) |
![]() | Dispose | オーバーロードされます。 ( Menu から継承されます。) |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 ( Component から継承されます。) |
![]() | FindMergePosition | メニューにおけるメニュー項目の追加位置を返します。 ( Menu から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 ( Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |
![]() | OnCollapse | Collapse イベントを発生させます。 |
![]() | OnPopup | Popup イベントを発生させます。 |
![]() | ProcessCmdKey | オーバーロードされます。 |

ContextMenu メンバ
ショートカット メニューを表します。ContextMenuStrip では、以前のバージョンの ContextMenu コントロールの機能が置換または追加されていますが、下位互換性を維持し、必要に応じて今後も使用できるように、ContextMenu も残されています。
ContextMenu データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | Container | Component を格納している IContainer を取得します。(Component から継承されます。) |
![]() | Handle | メニューのウィンドウ ハンドルを表している値を取得します。(Menu から継承されます。) |
![]() | IsParent | このメニューにメニュー項目が格納されているかどうかを示す値を取得します。このプロパティは読み取り専用です。(Menu から継承されます。) |
![]() | MdiListItem | マルチ ドキュメント インターフェイス (MDI) 子フォームの一覧を表示するために使用される MenuItem を示す値を取得します。(Menu から継承されます。) |
![]() | MenuItems | メニューに関連付けられている MenuItem オブジェクトのコレクションを示す値を取得します。(Menu から継承されます。) |
![]() | Name | Menu の名前を取得または設定します。(Menu から継承されます。) |
![]() | Site | Component の ISite を取得または設定します。(Component から継承されます。) |
![]() ![]() | Tag | コントロールに関連付けられたユーザー定義のデータを取得または設定します。(Menu から継承されます。) |

名前 | 説明 | |
---|---|---|
![]() | CanRaiseEvents | コンポーネントがイベントを発生させることがきるかどうかを示す値を取得します。(Component から継承されます。) |
![]() | DesignMode | Component が現在デザイン モードかどうかを示す値を取得します。(Component から継承されます。) |
![]() | Events | Component に結び付けられているイベント ハンドラのリストを取得します。(Component から継承されます。) |


名前 | 説明 | |
---|---|---|
![]() | CloneMenu | 現在の Menu にパラメータとして渡された Menu をコピーします。 (Menu から継承されます。) |
![]() | CreateMenuHandle | Menu への新しいハンドルを作成します。 (Menu から継承されます。) |
![]() | Dispose | オーバーロードされます。 ( Menu から継承されます。) |
![]() | Finalize | Component がガベージ コレクションによってクリアされる前に、アンマネージ リソースを解放し、その他のクリーンアップ操作を実行します。 (Component から継承されます。) |
![]() | FindMergePosition | メニューにおけるメニュー項目の追加位置を返します。 (Menu から継承されます。) |
![]() | GetService | Component またはその Container で提供されるサービスを表すオブジェクトを返します。 (Component から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |
![]() | OnCollapse | Collapse イベントを発生させます。 |
![]() | OnPopup | Popup イベントを発生させます。 |
![]() | ProcessCmdKey | オーバーロードされます。 |

名前 | 説明 | |
---|---|---|
![]() | Disposed | コンポーネントの Disposed イベントを待機するイベント ハンドラを追加します。(Component から継承されます。) |
![]() | Popup | ショートカット メニューが表示される前に発生します。 |

Weblioに収録されているすべての辞書からContextMenuを検索する場合は、下記のリンクをクリックしてください。

- ContextMenuのページへのリンク