ContextMenu.Popup イベント
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
ショートカット メニューが表示される前に発生します。
public:
event EventHandler ^ Popup;
public event EventHandler Popup;
member this.Popup : EventHandler
Public Custom Event Popup As EventHandler
イベントの種類
例
次のコード例では、ContextMenuのPopup イベントのイベント ハンドラーを作成します。 イベント ハンドラーのコードは、pictureBox1 という名前のPictureBoxと、ショートカット メニューを表示するコントロールtextBox1という名前のTextBoxの 2 つのコントロールを決定します。
ContextMenuがショートカット メニューを表示する原因となったコントロールに応じて、コントロールは適切なMenuItem オブジェクトをContextMenuに追加します。 この例では、contextMenu1という名前のContextMenu クラスのインスタンスをフォーム内で定義する必要があります。 この例では、フォームに TextBox と PictureBox を追加し、これらのコントロールの ContextMenu プロパティを contextMenu1に設定する必要もあります。
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(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 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
注釈
このイベントを使用して、 MenuItem オブジェクトを表示する前に初期化できます。 たとえば、3 つのTextBox コントロールにContextMenuを使用し、ショートカット メニューを表示しているTextBoxに応じて、ContextMenuの特定のメニュー項目を無効にする場合は、このイベントのイベント ハンドラーを作成できます。 SourceControl プロパティを使用して、ContextMenuを表示しようとしているTextBoxを特定し、適切なMenuItem オブジェクトを無効にすることができます。
イベントの処理の詳細については、「処理とイベントの発生」を参照してください。