Add a labeled graphic to the map

Discussion created by mvmartinez on Jul 10, 2012
Latest reply on Jul 17, 2012 by AlBowe
I'm attempting to create a button that will allow a user to add a labeled (coordinates) point (graphic) to a map. I've taken bits and pieces and brought them together. I can plot a point now but when the script gets to the point of labeling it crashes. Below is my code, please let me know what I have mixed up and perhaps what concept am I missing here.

    Protected Overrides Sub OnMouseDown(arg As ESRI.ArcGIS.Desktop.AddIns.Tool.MouseEventArgs)

        ' Get graphics container
        Dim pMXDocument As IMxDocument
        pMXDocument = My.ArcMap.Document
        Dim pActiveView As IActiveView
        pActiveView = pMXDocument.ActiveView
        Dim pGraphicsContainer As IGraphicsContainer
        pGraphicsContainer = pActiveView.GraphicsContainer

        ' Create green colour
        Dim pRGB As IRgbColor
        pRGB = New RgbColor
        With pRGB
            .Red = 84
            .Green = 255
            .Blue = 255
        End With

        'Current location of mouse point in map units
        Dim pPoint As IPoint
        pPoint = New ESRI.ArcGIS.Geometry.Point
        pPoint = My.ArcMap.Document.CurrentLocation

        ' Create point element

        Dim pPointEl As IMarkerElement = New MarkerElement

        'Create text element
        Dim ptextElement As ITextElement = New TextElementClass()

        Dim x = My.ArcMap.Document.CurrentLocation.X
        Dim y = My.ArcMap.Document.CurrentLocation.Y

        ptextElement.Text = x & ", " & y

        ' Create Element and set geometry
        Dim pElement As IElement = pPointEl
        pElement.Geometry = pPoint
        'pElement = ptextElement

        ' Create Point symbol (thick green line)
        Dim pPointSymbol As ISimpleMarkerSymbol
        pPointSymbol = New SimpleMarkerSymbol

        With pPointSymbol
            .Color = pRGB
            .Size = 10
        End With

        'Set font
        Dim pFormattedTextSymbol As IFormattedTextSymbol = New TextSymbolClass()

        With pFormattedTextSymbol
            .HorizontalAlignment = GetHorizontalAlignment()
            .VerticalAlignment = GetVerticalAlignment()
            With .Font
                .Name = "Times New Roman"
                .Size = 23
                .Bold = True
            End With
        End With

        ptextElement.Symbol = pFormattedTextSymbol
        pPointEl.Symbol = pPointSymbol

        ' Add to maps graphic container and refresh screen
        pGraphicsContainer.AddElement(pElement, 0)
        pGraphicsContainer.AddElement(ptextElement, 0)
        pGraphicsContainer = My.ArcMap.Document.FocusMap

        My.ArcMap.Document.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)

    End Sub