ContextMenu.SourceControl プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

ショートカット メニューを表示しているコントロールを表す Control。ショートカット メニューを表示しているコントロールがない場合、このプロパティは null 参照 (Visual Basic では Nothing) を返します。

このプロパティを使用すると、ContextMenu で定義されたショートカット メニューを現在表示しているコントロールを確認できます。ショートカット メニューが現在表示されていない場合は、このプロパティを使用して、どのコントロールがショートカット メニューを最後に表示したかを確認できます。このプロパティを Popup イベントで使用すると、コントロールで適切なメニュー項目を表示できます。また、このプロパティを使用して、コントロールへの参照をメソッドに渡し、ショートカット メニューで表示されたメニュー コマンドに関連付けられているタスクを実行することもできます。Form クラスは Control から継承されるため、ContextMenu がフォームに関連付けられている場合にもこのプロパティを使用できます。

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

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.SourceControl プロパティを検索する場合は、下記のリンクをクリックしてください。

- ContextMenu.SourceControl プロパティのページへのリンク