private void Button1_Click (object sender, System.EventArgs e)
{
Get the GraphicsContainer
Igraphicscontainer GraphicsContainer = Axpagelayoutcontrol1.graphicscontainer;
Get the Mapframe
Imapframe mapframe = (imapframe) graphicscontainer.findframe (AXPAGELAYOUTCONTROL1.ACTIVEVIEW.FOCUSMAP);
if (Mapframe = = null) return;
Create a legend
UID uid = new Uidclass ();
Uid.value = "Esricarto.legend";
Create a mapsurroundframe from the Mapframe
Imapsurroundframe mapsurroundframe = mapframe.createsurroundframe (UID, NULL);
if (Mapsurroundframe = = null) return;
if (Mapsurroundframe.mapsurround = = null) return;
Set the name
MapSurroundFrame.MapSurround.Name = "Legend";
Envelope for the legend
Ienvelope envelope = new Envelopeclass ();
Envelope. Putcoords (1, 1, 3.4, 2.4);
Set the geometry of the Mapsurroundframe
ielement element = (ielement) mapsurroundframe;
Element. Geometry = envelope;
ADD the legend to the pagelayout
Axpagelayoutcontrol1.addelement (element, Type.Missing, Type.Missing, "Legend", 0);
Refresh the Pagelayoutcontrol
AxPageLayoutControl1.ActiveView.PartialRefresh (esriviewdrawphase.esriviewgraphics, NULL, NULL);
disable/enable buttons
cmddraw.enabled = false;
Cmddelete.enabled = true;
Cmdchangearea.enabled = true;
Cmdchangeline.enabled = true;
}
private void Button2_Click (object sender, System.EventArgs e)
{
Find The Legend
ielement element = Axpagelayoutcontrol1.findelementbyname ("Legend", 1);
if (element! = NULL)
{
Delete The Legend
Igraphicscontainer GraphicsContainer = Axpagelayoutcontrol1.graphicscontainer;
Graphicscontainer.deleteelement (Element);
Refresh the display
AxPageLayoutControl1.ActiveView.PartialRefresh (esriviewdrawphase.esriviewgraphics, NULL, NULL);
enable/disable buttons
Cmddraw.enabled = true;
cmddelete.enabled = false;
cmdchangearea.enabled = false;
cmdchangeline.enabled = false;
}
}
Add legend legend and delete legend