ArcGIS Pro 3.2 API Reference Guide
ArcGIS.Desktop.Layouts Namespace / Element Class / SetX Method
Double
Example

In This Topic
    SetX Method (Element)
    In This Topic
    Sets the X position of the element. This method must be called on the MCT. Use QueuedTask.Run.
    Syntax
    public void SetX( 
       double x
    )
    Public Sub SetX( _
       ByVal x As Double _
    ) 

    Parameters

    x
    Double
    Exceptions
    ExceptionDescription
    This method must be called within the lambda passed to QueuedTask.Run.
    This element requires its mapview to be active
    Remarks
    Placement for an element contained in graphics layer requires the mapview be active. To change the element X and Y consider SetAnchorPoint
    Example
    Element_GetSetX
    //Modify an element's X position.
    
    //Perform on the worker thread
    await QueuedTask.Run(() =>
    {
      double elmX = element.GetX();
      elmX = 4.25;
    
      element.SetX(elmX); //You don't have to get to set; a shortcut would be: element.SetX(4.25);
    
    });
    Create_ScaleBar
    //Create a scale bar for a specific map frame and assign a scale bar style item.
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference a North Arrow in a style
      StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(item => item.Name == "ArcGIS 2D");
      ScaleBarStyleItem sbStyleItm = stylePrjItm.SearchScaleBars("Double Alternating Scale Bar 1")[0];
    
      //Build geometry
      Coordinate2D center = new Coordinate2D(7, 8);
    
      //Reference MF, create north arrow and add to layout 
      MapFrame mf = layout.FindElement("New Map Frame") as MapFrame;
      if (mf == null)
      {
        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Map frame not found", "WARNING");
        return;
      }
    
      //At 2.x -
      //ScaleBar sbElm = LayoutElementFactory.Instance.CreateScaleBar(layout, center, mf, sbStyleItm);
      //sbElm.SetName("New Scale Bar");
      //sbElm.SetWidth(2);
      //sbElm.SetX(6);
      //sbElm.SetY(7.5); 
    
      var sbInfo = new ScaleBarInfo()
      {
        MapFrameName = mf.Name,
        ScaleBarStyleItem = sbStyleItm
      };
      var sbElm = ElementFactory.Instance.CreateMapSurroundElement(
                                          layout, center.ToMapPoint(), sbInfo, "New Scale Bar") as ScaleBar;
      sbElm.SetWidth(2);
      sbElm.SetX(6);
      sbElm.SetY(7.5);
    });
    Create_NorthArrow
    //Create a north arrow for a specific map frame and assign a north arrow style item.
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Reference a North Arrow in a style
      StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>().FirstOrDefault(item => item.Name == "ArcGIS 2D");
      NorthArrowStyleItem naStyleItm = stylePrjItm.SearchNorthArrows("ArcGIS North 10")[0];
    
      //Build geometry
      Coordinate2D center = new Coordinate2D(7, 5.5);
    
      //Reference MF, create north arrow and add to layout 
      MapFrame mf = layout.FindElement("New Map Frame") as MapFrame;
      if (mf == null)
      {
        ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show("Map frame not found", "WARNING");
        return;
      }
      //At 2.x -
      //NorthArrow arrowElm = LayoutElementFactory.Instance.CreateNorthArrow(layout, center, mf, naStyleItm);
      //arrowElm.SetName("New North Arrow");
      //arrowElm.SetHeight(1.75);
      //arrowElm.SetX(7);
      //arrowElm.SetY(6);
    
      var naInfo = new NorthArrowInfo()
      {
        MapFrameName = mf.Name,
        NorthArrowStyleItem = naStyleItm
      };
    
      var arrowElm = ElementFactory.Instance.CreateMapSurroundElement(
                                layout, center.ToMapPoint(), naInfo, "New North Arrow") as NorthArrow;
      arrowElm.SetHeight(1.75);
      arrowElm.SetX(7);
      arrowElm.SetY(6);
    });
    CloneGraphic
    //This example finds a layout element by name and clones it a specified number of times and applies an accumlative offset for each 
    //cloned element.
    
    //Added references
    using ArcGIS.Core.CIM;                             //CIM
    using ArcGIS.Desktop.Core;                         //Project
    using ArcGIS.Desktop.Layouts;                      //Layout class
    using ArcGIS.Desktop.Framework.Threading.Tasks;    //QueuedTask
    
    public class GraphicElementExample2
    {
      public static Task<bool> CloneElementAsync(string LayoutName, string ElementName, double offset, int numCopies)
      {
        //Reference a layoutitem in a project by name
        LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>().FirstOrDefault(item => item.Name.Equals(LayoutName));
        if (layoutItem == null)
          return Task.FromResult(false);
    
        return QueuedTask.Run<bool>(() =>
        {
          //Reference and load the layout associated with the layout item
          Layout lyt = layoutItem.GetLayout();
    
          //Reference a element by name
          GraphicElement graElm = lyt.FindElement(ElementName) as GraphicElement;
          if (graElm == null)
            return false;
    
          //Loop through the number of copies, clone, and set the offsets for each new element
          double orig_offset = offset;
          while (numCopies != 0)
          {
            GraphicElement cloneElm = graElm.Clone("Clone");
            cloneElm.SetX(cloneElm.GetX() + offset);
            cloneElm.SetY(cloneElm.GetY() - offset);
            offset = offset + orig_offset;
            numCopies = numCopies - 1;
          }
    
          return true;
        });
      }
    }
    Create Point Text Element 1
    //Create a simple point text element and assign basic symbology and text settings.
    
    //Construct on the worker thread
    await QueuedTask.Run(() =>
    {
      //Build 2D point geometry
      Coordinate2D coord2D = new Coordinate2D(3.5, 10);
    
      //Set symbolology, create and add element to layout
      CIMTextSymbol sym = SymbolFactory.Instance.ConstructTextSymbol(
                    ColorFactory.Instance.RedRGB, 32, "Arial", "Regular");
      string textString = "Point text";
      //At 2.x - GraphicElement ptTxtElm =
      //         LayoutElementFactory.Instance.CreatePointTextGraphicElement(
      //                           layout, coord2D, textString, sym);
      //ptTxtElm.SetName("New Point Text");
      //ptTxtElm.SetAnchor(Anchor.CenterPoint);
      //ptTxtElm.SetX(4.5);
      //ptTxtElm.SetY(9.5);
      //ptTxtElm.SetRotation(45);
    
      //use ElementInfo to set placement properties during create
      var elemInfo = new ElementInfo()
      {
        Anchor = Anchor.CenterPoint,
        Rotation = 45
      };
      var ptTxtElm = ElementFactory.Instance.CreateTextGraphicElement(
        container, TextType.PointText, coord2D.ToMapPoint(), sym, textString,
                           "New Point Text", true, elemInfo);
    
      //Change additional text properties
      ptTxtElm.SetX(4.5);
      ptTxtElm.SetY(9.5);
    });
    Create North Arrow From StyleItem 2
    //Must be on QueuedTask.Run(() => { ...
    
    //Build geometry
    Coordinate2D center = new Coordinate2D(7, 5.5);
    
    //Reference a North Arrow in a style
    StyleProjectItem stylePrjItm = Project.Current.GetItems<StyleProjectItem>()
                                   .FirstOrDefault(item => item.Name == "ArcGIS 2D");
    NorthArrowStyleItem naStyleItm = stylePrjItm.SearchNorthArrows(
                                           "ArcGIS North 10")[0];
    
    //Reference MF, create north arrow and add to layout 
    //var mf = container.FindElement("New Map Frame") as MapFrame;
    var mf = layout.FindElement(MapFrameName) as MapFrame;
    var narrow_info = new NorthArrowInfo()
    {
      MapFrameName = mf.Name,
      NorthArrowStyleItem = naStyleItm
    };
    var arrowElm = (NorthArrow)ElementFactory.Instance.CreateMapSurroundElement(
      layout, center.ToMapPoint(), narrow_info) as NorthArrow;
    arrowElm.SetName("New North Arrow");
    arrowElm.SetHeight(1.75);
    arrowElm.SetX(7);
    arrowElm.SetY(6);
    Update text element properties
    //Update text element properties for an existing text element.
    
    // Reference a layoutitem in a project by name
    LayoutProjectItem layoutItem = Project.Current.GetItems<LayoutProjectItem>()
                          .FirstOrDefault(item => item.Name.Equals("MyLayout"));
    if (layoutItem != null)
    {
    
      //Perform on the worker thread
      QueuedTask.Run(() =>
      {
        // Reference and load the layout associated with the layout item
        Layout layout = layoutItem.GetLayout();
        if (layout != null)
        {
          // Reference a text element by name
          TextElement txtElm = layout.FindElement("MyTextElement") as TextElement;
          if (txtElm != null)
          {
            // Change placement properties
            txtElm.SetAnchor(Anchor.CenterPoint);
            txtElm.SetX(x);
            txtElm.SetY(y);
    
            // Change TextProperties
            TextProperties txtProperties = new TextProperties(
                             "Hello world", "Times New Roman", 48, "Regular");
            txtElm.SetTextProperties(txtProperties);
          }
        }
      });
    }
    Clone an element
    //Clone a layout graphic element and apply an offset.
    
    //Perform on the worker thread
    QueuedTask.Run(() =>
    {
      // Reference and load the layout associated with the layout item
      Layout layout = layoutItem.GetLayout();
      if (layout != null)
      {
        // Reference a graphic element by name
        GraphicElement graphicElement = 
                            layout.FindElement("MyElement") as GraphicElement;
        if (graphicElement != null)
        {
    
          //Clone and set the new x,y
          GraphicElement cloneElement = graphicElement.Clone("Clone");
          cloneElement.SetX(cloneElement.GetX() + xOffset);
          cloneElement.SetY(cloneElement.GetY() + yOffset);
        }
      }
    });
    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3 or higher.
    See Also