Add the "right-click to view Property Information" menu in TOC

Source: Internet
Author: User

(1) first, we create the frmAttribute class, that is, the dialog box for storing attribute information.

Private AxMapControl m_Mapctr;

Private ILayer m_pLyr;

 

Public frmAttribute (AxMapControl pMapCtr, ILayer pLyr) // when this class is created, the parameters "pMapCtr" and "pLyr" are accepted )"
{
InitializeComponent ();
M_Mapctr = pMapCtr;
M_pLyr = pLyr;
}

Private void frmAttribute_Load (object sender, EventArgs e)
{
Try
{
ILayer pLayer;
PLayer = m_pLyr;
IFeatureLayer pFLayer = pLayer as IFeatureLayer;
IFeatureClass pFC = pFLayer. FeatureClass;

ILayerFields pLayerFields = pFLayer as ILayerFields;
DataSet ds = new DataSet ("dsTest ");
DataTable dt = new DataTable (pFLayer. Name );
// Obtain the column name
DataColumn dc = null;
For (int I = 0; I <pLayerFields. FieldCount; I ++)
{
Dc = new DataColumn (pLayerFields. get_Field (I). Name );
Dt. Columns. Add (dc );
Dc = null;
}

// Obtain the Attribute Table

IFeatureCursor pFCursor = pFC. Search (null, false );
IFeature pFeature = pFCursor. NextFeature ();
While (pFeature! = Null)
{
DataRow dr = dt. NewRow ();

For (int j = 0; j <pLayerFields. FieldCount; j ++)
{
If (pLayerFields. FindField (pFC. ShapeFieldName) = j)
{
Dr [j] = pFC. ShapeType. ToString ();
}
Else
{
Dr [j] = pFeature. get_Value (j). ToString ();
}
}
Dt. Rows. Add (dr );

PFeature = pFCursor. NextFeature ();
}

// Display information

DataGridView1.DataSource = dt;
}
Catch (Exception exception)
{
MessageBox. Show ("failed to read Attribute Table:" + exception. Message );
}

}

 

(2) When the right-click TOC menu is displayed, record the selected information:

// To work with the toc context menu
Private esriTOCControlItem toccItem = esriTOCControlItem. esriTOCControlItemNone;
Private IBasicMap pBasicMap = null;

Private ILayer pLayer = null;

 

// Right-click the toc layer and choose

Private void axtoccontrolpoliconmousedown (object sender, ITOCControlEvents_OnMouseDownEvent e)
{
Object unk = null;
Object data = null;
If (e. button = 2)
{

// This function is the most critical. It finds the "position (e. x, e. x. y)" "layer (pLayer)" "type (toccItem )"
AxTOCControl1.HitTest (e. x, e. y, ref toccItem, ref pBasicMap, ref pLayer, ref unk, ref data );
If (toccItem = esriTOCControlItem. esriTOCControlItemLayer)
{
PFLayer = pLayer as IFeatureLayer;
PFC = pFLayer. FeatureClass;
ContextMenuStrip2.Show (axTOCControl1, new System. Drawing. Point (e. x, e. y ));
}
Else if (toccItem = esriTOCControlItem. esriTOCControlItemMap)
{
ContextMenuStrip2.Show (axTOCControl1, new System. Drawing. Point (e. x, e. y ));
}
}
}

Supplement: private esriTOCControlItem toccItem = esriTOCControlItem. esriTOCControlItemNone;

Here, toccItem has many types: esriTOCControlItemHeading (subject), esriTOCControlItemMap (Map Name ),

EsriTOCControlItemLayer (layer name), esriTOCControlItemLegendClass (layer legend ),

EsriTOCControlItemNone (blank areas other than heading, map, layer, and legend in TOC)

 

(3) In the last step, create a frmAttribute instance in Form1:

Private void attribute ToolStripMenuItem_Click (object sender, EventArgs e) // open the attribute window on Toc
{
FrmAttribute frm = new frmAttribute (axMapControl1, pLayer );
Frm. ShowDialog ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.