CodeClass2::AddEvent Method (String^, String^, Boolean, Object^, vsCMAccess)

 

Adds a class event.

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

CodeEvent^ AddEvent(
	String^ Name,
	String^ FullDelegateName,
	bool CreatePropertyStyleEvent = false,
	Object^ Location,
	vsCMAccess Access = vsCMAccess::vsCMAccessDefault
)

Parameters

Name
Type: System::String^

Required. Name of the class event to add.

FullDelegateName
Type: System::String^

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

CreatePropertyStyleEvent
Type: System::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.

Position

Optional. Default = 0. The code element after which to add the new event handler.

If the value is a Long, then Position indicates the location of the element after which to add the new element.

Because automation collections begin their count at 1, passing 0 indicates that the new event handler should be placed at the beginning of the class. A value of -1 means the function should be placed at the end.

Access
Type: EnvDTE::vsCMAccess

Optional. A vsCMAccess constant.

Return Value

Type: EnvDTE80::CodeEvent^

Returns the class event handler that was created.

System_CAPS_noteNote

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).

[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);
    }
}
Return to top
Show: