ContextMenu クラス
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)
構文
Public Class ContextMenu Inherits Menu
public class ContextMenu extends Menu
public class ContextMenu extends Menu
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 によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。
参照
Weblioに収録されているすべての辞書からContextMenu クラスを検索する場合は、下記のリンクをクリックしてください。
全ての辞書からContextMenu クラスを検索
- ContextMenu クラスのページへのリンク