Menu.GetContextMenu メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > Menu.GetContextMenu メソッドの意味・解説 

Menu.GetContextMenu メソッド

メニュー格納している ContextMenu取得します

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

public ContextMenu GetContextMenu ()
public:
ContextMenu^ GetContextMenu ()
public ContextMenu GetContextMenu ()

戻り値
メニュー格納している ContextMenu既定値null 参照 (Visual Basic では Nothing) です。

解説解説
使用例使用例

この例では、GetContextMenu メソッド使用して menuItem1 または menuItem2 を含むショートカット メニューへの参照取得しメッセージ ボックスショートカット メニューに関する情報表示しますプログラムによって、2 つの項目、New および Open のあるショートカット メニュー作成します次に適切なイベント ハンドラ作成することによって、これらの項目に機能追加します。この例を実行すると、フォーム右クリックしてショートカット メニュー表示するように指示するメッセージ ボックス表示されます。次にメニュー項目をクリックすると、どの項目がクリックされたかを通知するメッセージ表示されます。このメッセージには、格納しているショートカット メニューに関する情報示されています。この例では、Form1 という名前の Form が既に作成されている必要があります

Public Sub AddContextmenu()
    ' Create a shortcut menu.
    Dim m As New ContextMenu()
    Me.ContextMenu = m

    ' Create MenuItem objects.
    Dim menuItem1 As New
 MenuItem()
    Dim menuItem2 As New
 MenuItem()

    ' Set the Text property.
    menuItem1.Text = "New"
    menuItem2.Text = "Open"

    ' Add menu items to the MenuItems collection.
    m.MenuItems.Add(menuItem1)
    m.MenuItems.Add(menuItem2)

    ' Display the starting message.
    MessageBox.Show("Right-click the form to display the shortcut
 menu items")

    ' Add functionality to the menu items. 
    AddHandler menuItem1.Click, AddressOf Me.menuItem1_Click
    AddHandler menuItem2.Click, AddressOf Me.menuItem2_Click

End Sub 'AddContextmenu


Private Sub menuItem1_Click(ByVal
 sender As Object, ByVal
 e As System.EventArgs)
    Dim textReport As String
 = "You clicked the New menu item. " + vbCr + "It
 is contained in the following shortcut menu: " + vbCr + vbCr

    ' Get information on the shortcut menu in which menuitem1 is contained.
    textReport += ContextMenu.GetContextMenu().ToString()

    ' Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information")
End Sub 'menuItem1_Click


Private Sub menuItem2_Click(ByVal
 sender As Object, ByVal
 e As System.EventArgs)
    Dim textReport As String
 = "You clicked the Open menu item. " + vbCr + "It
 is contained in the following shortcut menu: " + vbCr + vbCr

    ' Get information on the shortcut menu in which menuitem1 is contained.
    textReport += ContextMenu.GetContextMenu().ToString()

    ' Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information")
End Sub 'menuItem2_Click
public void AddContextmenu()
{
    // Create a shortcut menu.
    ContextMenu m = new ContextMenu();
    this.ContextMenu= m;

    // Create MenuItem objects.
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();
    
    // Set the Text property.
    menuItem1.Text = "New";
    menuItem2.Text = "Open";

    // Add menu items to the MenuItems collection.
    m.MenuItems.Add(menuItem1);
    m.MenuItems.Add(menuItem2);

    // Display the starting message.
    MessageBox.Show("Right-click the form to display the shortcut menu items");


    // Add functionality to the menu items. 
    menuItem1.Click += new System.EventHandler(this.menuItem1_Click);
    menuItem2.Click += new System.EventHandler(this.menuItem2_Click);
    }

private void menuItem1_Click(object sender,
 System.EventArgs e)
{
    string textReport =    "You clicked the New menu item.
 \n" +
        "It is contained in the following shortcut menu:
 \n\n"; 

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += ContextMenu.GetContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport,"The ContextMenu Information");        
}

private void menuItem2_Click(object sender,
 System.EventArgs e)
{
    string textReport =    "You clicked the Open menu item.
 \n" +
        "It is contained in the following shortcut menu:
 \n\n"; 

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += ContextMenu.GetContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport,"The ContextMenu Information");        
}
public:
   [STAThread]
   void AddContextmenu()
   {
      // Create a shortcut menu.
      System::Windows::Forms::ContextMenu^ m = gcnew System::Windows::Forms::ContextMenu;
      this->ContextMenu = m;

      // Create MenuItem objects.
      MenuItem^ menuItem1 = gcnew MenuItem;
      MenuItem^ menuItem2 = gcnew MenuItem;

      // Set the Text property.
      menuItem1->Text = "New";
      menuItem2->Text = "Open";

      // Add menu items to the MenuItems collection.
      m->MenuItems->Add( menuItem1 );
      m->MenuItems->Add( menuItem2 );

      // Display the starting message.
      MessageBox::Show( "Right-click the form to display the shortcut menu items"
 );

      // Add functionality to the menu items. 
      menuItem1->Click += gcnew System::EventHandler( this,
 &Form1::menuItem1_Click );
      menuItem2->Click += gcnew System::EventHandler( this,
 &Form1::menuItem2_Click );
   }

private:
   void menuItem1_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      String^ textReport = "You clicked the New menu item. \n"
      "It is contained in the following shortcut menu: \n\n";

      // Get information on the shortcut menu in which menuitem1 is
 contained.
      textReport = String::Concat( textReport, this->ContextMenu->GetContextMenu()->ToString()
 );

      // Display the shortcut menu information in a message box.
      MessageBox::Show( textReport, "The ContextMenu Information" );
   }

   void menuItem2_Click( Object^ /*sender*/, System::EventArgs^
 /*e*/ )
   {
      String^ textReport = "You clicked the Open menu item. \n"
      "It is contained in the following shortcut menu: \n\n";

      // Get information on the shortcut menu in which menuitem1 is
 contained.
      textReport = String::Concat( textReport, this->ContextMenu->GetContextMenu()->ToString()
 );

      // Display the shortcut menu information in a message box.
      MessageBox::Show( textReport, "The ContextMenu Information" );
   }
    // Create a shortcut menu.
    ContextMenu m = new ContextMenu();
    this.set_ContextMenu(m);

    // Create MenuItem objects.
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();

    // Set the Text property.
    menuItem1.set_Text("New");
    menuItem2.set_Text("Open");

    // Add menu items to the MenuItems collection.
    m.get_MenuItems().Add(menuItem1);
    m.get_MenuItems().Add(menuItem2);

    // Display the starting message.
    MessageBox.Show("Right-click the form to display the shortcut menu "
 
        + " items");

    // Add functionality to the menu items. 
    menuItem1.add_Click(new System.EventHandler(this.menuItem1_Click));
    menuItem2.add_Click(new System.EventHandler(this.menuItem2_Click));
} //AddContextmenu

private void menuItem1_Click(Object sender,
 System.EventArgs e)
{
    String textReport = "You clicked the New menu item. \n" 
        + "It is contained in the following shortcut menu:
 \n\n";

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += get_ContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem1_Click

private void menuItem2_Click(Object sender,
 System.EventArgs e)
{
    String textReport = "You clicked the Open menu item. \n" 
        + "It is contained in the following shortcut menu:
 \n\n";

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += get_ContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem2_Click
public void AddContextmenu()
{
    // Create a shortcut menu.
    ContextMenu m = new ContextMenu();
    this.set_ContextMenu(m);

    // Create MenuItem objects.
    MenuItem menuItem1 = new MenuItem();
    MenuItem menuItem2 = new MenuItem();

    // Set the Text property.
    menuItem1.set_Text("New");
    menuItem2.set_Text("Open");

    // Add menu items to the MenuItems collection.
    m.get_MenuItems().Add(menuItem1);
    m.get_MenuItems().Add(menuItem2);

    // Display the starting message.
    MessageBox.Show("Right-click the form to display the shortcut menu "
 
        + " items");

    // Add functionality to the menu items. 
    menuItem1.add_Click(new System.EventHandler(this.menuItem1_Click));
    menuItem2.add_Click(new System.EventHandler(this.menuItem2_Click));
} //AddContextmenu

private void menuItem1_Click(Object sender,
 System.EventArgs e)
{
    String textReport = "You clicked the New menu item. \n" 
        + "It is contained in the following shortcut menu:
 \n\n";

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += get_ContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem1_Click

private void menuItem2_Click(Object sender,
 System.EventArgs e)
{
    String textReport = "You clicked the Open menu item. \n" 
        + "It is contained in the following shortcut menu:
 \n\n";

    // Get information on the shortcut menu in which menuitem1 is contained.
    textReport += get_ContextMenu().ToString();

    // Display the shortcut menu information in a message box.
    MessageBox.Show(textReport, "The ContextMenu Information");
} //menuItem2_Click
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からMenu.GetContextMenu メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からMenu.GetContextMenu メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からMenu.GetContextMenu メソッド を検索

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

辞書ショートカット

すべての辞書の索引

Menu.GetContextMenu メソッドのお隣キーワード
検索ランキング

   

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



Menu.GetContextMenu メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS