ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Desktop.Mapping Namespace / IElementContainer Interface / GetSelectedElements Method
Example

In This Topic
    GetSelectedElements Method (IElementContainer)
    In This Topic
    Gets the collection of currently selected elements
    Syntax
    IReadOnlyList<IElement> GetSelectedElements()
    Function GetSelectedElements() As IReadOnlyList(Of IElement)

    Return Value

    A list of 0 or more elements
    Example
    ElementEvent_SelectionChanged
    //Report the event args when a layout's selection has changed.
    
    //At 2.x - ArcGIS.Desktop.Layouts.Events.LayoutSelectionChangedEvent.Subscribe((args) => {
    //  var elements = args.Elements;
    //  string elemNames = "null";
    //  if (elements != null && elements.Count() > 0)
    //  {
    //    elemNames = string.Join(",", elements.Select(e => e.Name).ToList());
    //  }
    //  System.Windows.MessageBox.Show("LayoutSelectionChangedEvent:" +
    //  Environment.NewLine +
    //  "   arg Elements: " + elemNames.ToString() +
    //  Environment.NewLine +
    //  "   arg Layout: " + args.Layout.Name);
    //});
    
    ArcGIS.Desktop.Layouts.Events.ElementEvent.Subscribe((args) =>
    {
      if (args.Hint == ElementEventHint.SelectionChanged)
      {
        if (args.Container.GetSelectedElements()?.Count() > 0)
        {
          var elemNames = string.Join(",", args.Container.GetSelectedElements().Select(e => e.Name).ToList());
          System.Diagnostics.Debug.WriteLine($"ElementEventHint.SelectionChanged: {elemNames}");
        }
      }
    
    });
    ElementEvent_PropertyChanged
    //Report the event args when an element has been updated.
    
    //At 2.x - ArcGIS.Desktop.Layouts.Events.ElementsUpdatedEvent.Subscribe((args) => 
    //{
    //  var elements = args.Elements;
    //  string elemNames = "null";
    //  if (elements != null && elements.Count() > 0)
    //  {
    //    elemNames = string.Join(",", elements.Select(e => e.Name).ToList());
    //  }
    //  System.Windows.MessageBox.Show("ElementsUpdatedEvent:" +
    //  Environment.NewLine +
    //  "   arg Elements: " + elemNames.ToString() +
    //  Environment.NewLine +
    //  "   arg Layout: " + args.Layout.Name);
    //});
    ArcGIS.Desktop.Layouts.Events.ElementEvent.Subscribe((args) =>
    {
      string elemNames = "null";
      //can be null if the name was changed
      if (args.Elements?.Count() > 0)
      {
        elemNames = string.Join(",", args.Elements.Select(e => e.Name).ToList());
      }
      else if (args.Container.GetSelectedElements()?.Count() > 0)
      {
        elemNames = string.Join(",", args.Container.GetSelectedElements().Select(e => e.Name).ToList());
      }
    
      if (args.Hint == ElementEventHint.PropertyChanged)
      {
        System.Diagnostics.Debug.WriteLine($"ElementEventHint.PropertyChanged: {elemNames}");
      }
    });
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also