Freigeben über


CodeClass2.AddEvent Method

Adds a class event.

Namespace:  EnvDTE80
Assembly:  EnvDTE80 (in EnvDTE80.dll)

Syntax

'Declaration
Function AddEvent ( _
    Name As String, _
    FullDelegateName As String, _
    CreatePropertyStyleEvent As Boolean, _
    Location As Object, _
    Access As vsCMAccess _
) As CodeEvent
CodeEvent AddEvent(
    string Name,
    string FullDelegateName,
    bool CreatePropertyStyleEvent,
    Object Location,
    vsCMAccess Access
)
CodeEvent^ AddEvent(
    String^ Name, 
    String^ FullDelegateName, 
    [InAttribute] bool CreatePropertyStyleEvent, 
    Object^ Location, 
    vsCMAccess Access
)
abstract AddEvent : 
        Name:string * 
        FullDelegateName:string * 
        CreatePropertyStyleEvent:bool * 
        Location:Object * 
        Access:vsCMAccess -> CodeEvent
function AddEvent(
    Name : String, 
    FullDelegateName : String, 
    CreatePropertyStyleEvent : boolean, 
    Location : Object, 
    Access : vsCMAccess
) : CodeEvent

Parameters

  • Name
    Type: String

    Required. Name of the class event to add.

  • FullDelegateName
    Type: String

    Required. Name of the delegate to base the event on. This acts as a template for the new event handler.

  • CreatePropertyStyleEvent
    Type: Boolean

    Optional. Creates an event that has a property style accessor. True indicates that the event should have an accessor, false indicates that it should not.

  • Location
    Type: Object

Return Value

Type: EnvDTE80.CodeEvent
Returns the class event handler that was created.

Note

The values of code model elements such as classes, structs, functions, attributes, delegates, and so forth can be non-deterministic after making certain kinds of edits, meaning that their values cannot be relied upon to always remain the same. For more information, see the section Code Model Element Values Can Change in Discovering Code by Using the Code Model (Visual Basic).

Examples

[Visual Basic]

Sub AddEventExample(ByVal dte As DTE2)
    ' Before running this example, open a code document from a project
    ' and place the insertion point inside a class definition.
    Try
        ' Retrieve the CodeClass at the insertion point.
        Dim sel As TextSelection = _
            CType(dte.ActiveDocument.Selection, TextSelection)
        Dim cls As CodeClass = _
            CType(sel.ActivePoint.CodeElement( _
            vsCMElement.vsCMElementClass), CodeClass)

        ' Create a new event handler.
        cls.AddEvent("NewOnConnection", "OnConnection", True, -1, 
          vsCMAccess.vsCMAccessPublic)
    Catch ex As Exception
        MsgBox(ex.Message)
    End Try
End Sub

[C#]

public void AddEventExample(DTE2 dte)
{
    // Before running this example, open a code document from a project
    // and place the insertion point inside a class definition.
    try
    {
        // Retrieve the CodeClass at the insertion point.
        TextSelection sel = 
            (TextSelection)dte.ActiveDocument.Selection;
        CodeClass cls = 
            (CodeClass)sel.ActivePoint.get_CodeElement(
            vsCMElement.vsCMElementClass);
        // Creates a new event handler.
        cls.AddEvent("NewOnConnection", "OnConnection", true, -1, 
          vsCMAccess.vsCMAccessPublic);
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

.NET Framework Security

See Also

Reference

CodeClass2 Interface

EnvDTE80 Namespace

Other Resources

How to: Compile and Run the Automation Object Model Code Examples

Discovering Code by Using the Code Model (Visual Basic)

Discovering Code by Using the Code Model (Visual C#)