Per Mausklick bewerten und Feedback geben
MSDN
MSDN Library
.NET Entwicklung
.NET Framework 3.5
.NET Framework
Alle reduzieren/Alle erweitern Alle reduzieren
Diese Seite ist spezifisch für
Microsoft Visual Studio 2008/.NET Framework 3.5

Andere Versionen stehen ebenfalls zur Verfügung für:
.NET Framework-Klassenbibliothek
ContextMenuStrip-Klasse

Aktualisiert: November 2007

Stellt ein Kontextmenü dar.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Deklaration)
<ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)> _
<ComVisibleAttribute(True)> _
Public Class ContextMenuStrip _
    Inherits ToolStripDropDownMenu
Visual Basic (Verwendung)
Dim instance As ContextMenuStrip
C#
[ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch)]
[ComVisibleAttribute(true)]
public class ContextMenuStrip : ToolStripDropDownMenu
VisualC++
[ClassInterfaceAttribute(ClassInterfaceType::AutoDispatch)]
[ComVisibleAttribute(true)]
public ref class ContextMenuStrip : public ToolStripDropDownMenu
J#
/** @attribute ClassInterfaceAttribute(ClassInterfaceType.AutoDispatch) */
/** @attribute ComVisibleAttribute(true) */
public class ContextMenuStrip extends ToolStripDropDownMenu
Jscript
public class ContextMenuStrip extends ToolStripDropDownMenu

ContextMenuStrip ersetzt ContextMenu. Sie können jedem Steuerelement einen ContextMenuStrip zuordnen. Nach dem Klicken mit der rechten Maustaste wird dann das entsprechende Kontextmenü angezeigt. Sie können einen ContextMenuStrip programmgesteuert über die Show.Methode anzeigen. ContextMenuStrip unterstützt Opening- und Closing-Ereignisse, die abgebrochen werden können, um das dynamische Auffüllen und Szenarios mit mehreren Mausklicks zu behandeln. ContextMenuStrip unterstützt Bilder, den Aktivierungszustand von Menüelementen, Text, Zugriffstasten, Tastenkombinationen und Untermenüs.

Die folgenden Elemente wurden speziell für die problemlose Integration in ToolStripSystemRenderer und ToolStripProfessionalRenderer in allen Ausrichtungen entwickelt. Sie sind zur Entwurfszeit standardmäßig für das ContextMenuStrip-Steuerelement verfügbar:

Kontextmenüs werden i. d. R. verwendet, um verschiedene, für den Benutzer in einem bestimmten Kontext der Anwendung nützliche Menüelemente aus einem MenuStrip eines Formulars miteinander zu kombinieren. Sie können z. B. mithilfe eines einem TextBox-Steuerelement zugewiesenen Kontextmenüs Menübefehle zum Ändern der Textschriftart, zum Suchen nach Text innerhalb des Steuerelements oder Zwischenablagefeatures zum Kopieren und Einfügen von Text bereitstellen. Sie können außerdem in einem Kontextmenü neue ToolStripMenuItem-Objekte verfügbar machen, die sich nicht in einem MenuStrip befinden, um situationsspezifische Befehle bereitzustellen, deren Anzeige in einem MenuStrip inadäquat wäre.

Ein Kontextmenü wird i. d. R. angezeigt, wenn ein Benutzer mit der rechten Maustaste auf ein Steuerelement oder das Formular selbst klickt. Viele angezeigte Steuerelemente sowie das Form selbst weisen eine Control..::.ContextMenuStrip-Eigenschaft auf, die die ContextMenuStrip-Klasse an das Steuerelement bindet, das das Kontextmenü anzeigt. Ein ContextMenuStrip kann von mehr als einem Steuerelement verwendet werden.

Legen Sie die ToolStripDropDownMenu..::.ShowCheckMargin-Eigenschaft auf true fest, um links von einem ToolStripMenuItem Platz für eine Markierung hinzuzufügen, durch das angezeigt wird, das das Menüelement aktiviert oder ausgewählt ist. Die ToolStripDropDownMenu..::.ShowImageMargin-Eigenschaft ist standardmäßig auf true festgelegt. Verwenden Sie diesen Platz links vom ToolStripMenuItem, um ein Bild für dieses Menüelement anzuzeigen.

Der ContextMenuStrip ersetzt Funktionalität und fügt dem ContextMenu-Steuerelement früherer Versionen Funktionalität hinzu. Sie können das ContextMenu bei Bedarf jedoch aus Gründen der Abwärtskompatibilität und für eine künftige Verwendung beibehalten.

Im folgenden Codebeispiel wird ein ContextMenuStrip veranschaulicht, dem dynamisch Elemente hinzugefügt wird, in dem das SourceControl dynamisch ermittelt und wieder verwendet und das Opening-Ereignis behandelt wird.

Visual Basic
' This code example demonstrates how to handle the Opening event.
' It also demonstrates dynamic item addition and dynamic 
' SourceControl determination with reuse.
Class Form3
    Inherits Form

   ' Declare the ContextMenuStrip control.
   Private fruitContextMenuStrip As ContextMenuStrip


   Public Sub New()
      ' Create a new ContextMenuStrip control.
      fruitContextMenuStrip = New ContextMenuStrip()

      ' Attach an event handler for the 
      ' ContextMenuStrip control's Opening event.
      AddHandler fruitContextMenuStrip.Opening, AddressOf cms_Opening

      ' Create a new ToolStrip control.
      Dim ts As New ToolStrip()

      ' Create a ToolStripDropDownButton control and add it
      ' to the ToolStrip control's Items collections.
      Dim fruitToolStripDropDownButton As New ToolStripDropDownButton("Fruit", Nothing, Nothing, "Fruit")
      ts.Items.Add(fruitToolStripDropDownButton)

      ' Dock the ToolStrip control to the top of the form.
      ts.Dock = DockStyle.Top

      ' Assign the ContextMenuStrip control as the 
      ' ToolStripDropDownButton control's DropDown menu.
      fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip

      ' Create a new MenuStrip control and add a ToolStripMenuItem.
      Dim ms As New MenuStrip()
      Dim fruitToolStripMenuItem As New ToolStripMenuItem("Fruit", Nothing, Nothing, "Fruit")
      ms.Items.Add(fruitToolStripMenuItem)

      ' Dock the MenuStrip control to the top of the form.
      ms.Dock = DockStyle.Top

      ' Assign the MenuStrip control as the 
      ' ToolStripMenuItem's DropDown menu.
      fruitToolStripMenuItem.DropDown = fruitContextMenuStrip

      ' Assign the ContextMenuStrip to the form's 
      ' ContextMenuStrip property.
      Me.ContextMenuStrip = fruitContextMenuStrip

      ' Add the ToolStrip control to the Controls collection.
        Me.Controls.Add(ts)

        'Add a button to the form and assign its ContextMenuStrip.
        Dim b As New Button()
        b.Location = New System.Drawing.Point(60, 60)
        Me.Controls.Add(b)
        b.ContextMenuStrip = fruitContextMenuStrip

      ' Add the MenuStrip control last.
      ' This is important for correct placement in the z-order.
      Me.Controls.Add(ms)
    End Sub

   ' This event handler is invoked when the ContextMenuStrip
   ' control's Opening event is raised. It demonstrates
   ' dynamic item addition and dynamic SourceControl 
   ' determination with reuse.
    Sub cms_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)

        ' Acquire references to the owning control and item.
        Dim c As Control = fruitContextMenuStrip.SourceControl
        Dim tsi As ToolStripDropDownItem = fruitContextMenuStrip.OwnerItem 

        ' Clear the ContextMenuStrip control's 
        ' Items collection.
        fruitContextMenuStrip.Items.Clear()

        ' Check the source control first.
        If (c IsNot Nothing) Then
            ' Add custom item (Form)
            fruitContextMenuStrip.Items.Add(("Source: " + c.GetType().ToString()))
        ElseIf (tsi IsNot Nothing) Then
            ' Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
            fruitContextMenuStrip.Items.Add(("Source: " + tsi.GetType().ToString()))
        End If

        ' Populate the ContextMenuStrip control with its default items.
        fruitContextMenuStrip.Items.Add("-")
        fruitContextMenuStrip.Items.Add("Apples")
        fruitContextMenuStrip.Items.Add("Oranges")
        fruitContextMenuStrip.Items.Add("Pears")

        ' Set Cancel to false. 
        ' It is optimized to true based on empty entry.
        e.Cancel = False
    End Sub
End Class
C#
// This code example demonstrates how to handle the Opening event.
// It also demonstrates dynamic item addition and dynamic 
// SourceControl determination with reuse.
class Form3 : Form
{
    // Declare the ContextMenuStrip control.
    private ContextMenuStrip fruitContextMenuStrip;

    public Form3()
    {
        // Create a new ContextMenuStrip control.
        fruitContextMenuStrip = new ContextMenuStrip();

        // Attach an event handler for the 
        // ContextMenuStrip control's Opening event.
        fruitContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(cms_Opening);

        // Create a new ToolStrip control.
        ToolStrip ts = new ToolStrip();

        // Create a ToolStripDropDownButton control and add it
        // to the ToolStrip control's Items collections.
        ToolStripDropDownButton fruitToolStripDropDownButton = new ToolStripDropDownButton("Fruit", null, null, "Fruit");
        ts.Items.Add(fruitToolStripDropDownButton);

        // Dock the ToolStrip control to the top of the form.
        ts.Dock = DockStyle.Top;

        // Assign the ContextMenuStrip control as the 
        // ToolStripDropDownButton control's DropDown menu.
        fruitToolStripDropDownButton.DropDown = fruitContextMenuStrip;

        // Create a new MenuStrip control and add a ToolStripMenuItem.
        MenuStrip ms = new MenuStrip();
        ToolStripMenuItem fruitToolStripMenuItem = new ToolStripMenuItem("Fruit", null, null, "Fruit");
        ms.Items.Add(fruitToolStripMenuItem);

        // Dock the MenuStrip control to the top of the form.
        ms.Dock = DockStyle.Top;

        // Assign the MenuStrip control as the 
        // ToolStripMenuItem's DropDown menu.
        fruitToolStripMenuItem.DropDown = fruitContextMenuStrip;

        // Assign the ContextMenuStrip to the form's 
        // ContextMenuStrip property.
        this.ContextMenuStrip = fruitContextMenuStrip;

        // Add the ToolStrip control to the Controls collection.
        this.Controls.Add(ts);

        //Add a button to the form and assign its ContextMenuStrip.
        Button b = new Button();
        b.Location = new System.Drawing.Point(60, 60);
        this.Controls.Add(b);
        b.ContextMenuStrip = fruitContextMenuStrip;

        // Add the MenuStrip control last.
        // This is important for correct placement in the z-order.
        this.Controls.Add(ms);
    }

    // This event handler is invoked when the ContextMenuStrip
    // control's Opening event is raised. It demonstrates
    // dynamic item addition and dynamic SourceControl 
    // determination with reuse.
    void cms_Opening(object sender, System.ComponentModel.CancelEventArgs e)
    {
        // Acquire references to the owning control and item.
        Control c = fruitContextMenuStrip.SourceControl as Control;
        ToolStripDropDownItem tsi = fruitContextMenuStrip.OwnerItem as ToolStripDropDownItem;

        // Clear the ContextMenuStrip control's Items collection.
        fruitContextMenuStrip.Items.Clear();

        // Check the source control first.
        if (c != null)
        {
            // Add custom item (Form)
            fruitContextMenuStrip.Items.Add("Source: " + c.GetType().ToString());
        }
        else if (tsi != null)
        {
            // Add custom item (ToolStripDropDownButton or ToolStripMenuItem)
            fruitContextMenuStrip.Items.Add("Source: " + tsi.GetType().ToString());
        }

        // Populate the ContextMenuStrip control with its default items.
        fruitContextMenuStrip.Items.Add("-");
        fruitContextMenuStrip.Items.Add("Apples");
        fruitContextMenuStrip.Items.Add("Oranges");
        fruitContextMenuStrip.Items.Add("Pears");

        // Set Cancel to false. 
        // It is optimized to true based on empty entry.
        e.Cancel = false;
    }
}
System..::.Object
  System..::.MarshalByRefObject
    System.ComponentModel..::.Component
      System.Windows.Forms..::.Control
        System.Windows.Forms..::.ScrollableControl
          System.Windows.Forms..::.ToolStrip
            System.Windows.Forms..::.ToolStripDropDown
              System.Windows.Forms..::.ToolStripDropDownMenu
                System.Windows.Forms..::.ContextMenuStrip
Alle öffentlichen static (Shared in Visual Basic)-Member dieses Typs sind threadsicher. Bei Instanzmembern ist die Threadsicherheit nicht gewährleistet.

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

.NET Framework

Unterstützt in: 3.5, 3.0, 2.0
Communityinhalt   Was ist Community Content?
Neuen Inhalt hinzufügen RSS  Anmerkungen
Processing
© 2009 Microsoft Corporation. Alle Rechte vorbehalten. Nutzungsbedingungen | Markenzeichen | Informationen zur Datensicherheit
Page view tracker