(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 ();
}