C # Arcengine Toccontrol implementation right-click

Source: Internet
Author: User
Tags list of attributes

The first method: Use the ContextMenuStrip control

1. Create a new form attributetable, and define a global variable mlayer, so that the AXMAPCONTROL1 layer inside the main form is passed in to get the list of attributes!

       ILayer mlayer;          Public attributetable (ILayer layer)        {            InitializeComponent ();             = layer;        }

Loading the property sheet under the Load event of the Attributtetable form

 private void Attributetable_load (object sender, EventArgs e) {Ifeaturelayer Pfeaturelayer = Mlayer as            Ifeaturelayer;            Ifeatureclass pfeatureclass = Pfeaturelayer.featureclass;            DataTable dt = new DataTable ();                if (pfeatureclass! = null) {DataColumn DC; for (int i = 0; i < PFeatureClass.Fields.FieldCount; i++) {dc = new DataColumn (pfeat UreClass.Fields.get_Field (i).                    Name); Dt. Columns.Add (DC);//Get property values for all columns} ifeaturecursor pfeaturecursor = Pfeatureclass.search (NULL, FAL                SE);                IFeature pfeature = Pfeaturecursor.nextfeature ();                DataRow Dr; while (pfeature! = null) {dr = dt.                    NewRow ();                        for (int j = 0; J < PFeatureClass.Fields.FieldCount; J + +) {//Determine feature shapeif (PFeature.Fields.get_Field (j). Name = = "Shape") {if (PFeature.Shape.GeometryType = = ESRI. ArcGIS.Geometry.esriGeometryType.esriGeometryPoint) {Dr[j] = "point                            "; } if (PFeature.Shape.GeometryType = = ESRI.  ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline) {Dr[j] =                            "Line"; } if (PFeature.Shape.GeometryType = = ESRI. ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon) {Dr[j] =                            "Face"; }} else {dr[j] = Pfeatur E.get_value (j). ToString ();//Add line}} dt.                    Rows.Add (DR); Pfeature = Pfeaturecursor.nextfeature ();            } datagridview1.datasource = dt; }        }

2. Create a right-click menu (This example just does an open attribute table operation), in:

Define a common variable within the main window: public ILayer layer;

Under the Axtoccontrol1_onmousedown event

private void Axtoccontrol1_onmousedown (object sender, Itoccontrolevents_onmousedownevent e)        {            if (E.button = = 2)            {                Esritoccontrolitem item = esritoccontrolitem.esritoccontrolitemnone;                Ibasicmap map = new Mapclass ();                Layer = new Featurelayerclass ();                Object other = new Object ();                Object index = new object ();                Axtoccontrol1.hittest (e.x, E.Y, ref item, ref map, ref layer, ref other, ref index); To implement the assignment, the parameter of ref must initialize                if (item = = Esritoccontrolitem.esritoccontrolitemlayer)////Click on the layer, it will display the right-click menu                {                    Contextmenustrip1.show (AxTOCControl1, New System.Drawing.Point (e.x, e.y));}}}        

3. Click ContextMenuStrip1, displays the property sheet.

private void Contextmenustrip1_click (object sender, EventArgs e)        {            attributetable attributetable = new AttributeTable (layer);            Attributetable.text = "attribute table:" + layer. Name;            Attributetable.showdialog ();        }

The effect is as follows:

The first method: Use the base Command class

1. Create a new base command class openattributetable and define a global variable, private ILayer M_layer, with the following code:

        Private ILayer M_layer;        Public openattributetable (ILayer pLayer)        {            base.m_category = "";             base.m_caption = "open attribute table";              Base.m_message = "";              Base.m_tooltip = "";              Base.m_name = "";               M_layer = PLayer;            Try            {//Load additional picture                string bitmapresourcename = GetType (). Name + ". bmp";                Base.m_bitmap = new Bitmap (GetType (), bitmapresourcename);            }            catch (Exception ex)            {                System.Diagnostics.Trace.WriteLine (ex. Message, "Invalid Bitmap");            }        }

Then load the property sheet under its Click event, with the following code:

public override void OnClick () {//Todo:add Openattributetable.onclick implementation Ifeat            Urelayer Pfeaturelayer = M_layer as Ifeaturelayer;            Ifeatureclass pfeatureclass = Pfeaturelayer.featureclass;            DataTable dt = new DataTable ();                if (pfeatureclass! = null) {DataColumn DC; for (int i = 0; i < PFeatureClass.Fields.FieldCount; i++) {dc = new DataColumn (pfeat UreClass.Fields.get_Field (i).                    Name); Dt.                Columns.Add (DC);                } ifeaturecursor pfeaturecursor = Pfeatureclass.search (null, FALSE);                IFeature pfeature = Pfeaturecursor.nextfeature ();                DataRow Dr; while (pfeature! = null) {dr = dt.                    NewRow (); for (int j = 0; J < PFeatureClass.Fields.FieldCount; J + +) {if (pfeature.Fields.get_field (j). Name = = "Shape") {if (PFeature.Shape.GeometryType = = ESRI. ArcGIS.Geometry.esriGeometryType.esriGeometryPoint) {Dr[j] = "point                            "; } if (PFeature.Shape.GeometryType = = ESRI.  ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline) {Dr[j] =                            "Line"; } if (PFeature.Shape.GeometryType = = ESRI. ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon) {Dr[j] =                            "Face"; }} else {dr[j] = Pfeatur E.get_value (j).                        ToString (); }} dt.                    Rows.Add (DR); Pfeature = PfeaturecurSor.                Nextfeature ();                } attributetable at = new AttributeTable (); At.                Text = "attribute table:" + pfeaturelayer.name; At.                Show ();            AT.dataGridView1.DataSource = DT; }        }

2. Complete the Click event under the Axtoccontrol1_onmousedown event with the following code:

private void Axtoccontrol1_onmousedown (object sender, Itoccontrolevents_onmousedownevent e) {if (E.butt                on = = 2) {Esritoccontrolitem item = Esritoccontrolitem.esritoccontrolitemnone;                Ibasicmap map = new Mapclass ();                Layer = new Featurelayerclass ();                Object other = new Object ();                Object index = new object ();                Axtoccontrol1.hittest (e.x, E.Y, ref item, ref map, ref layer, ref other, ref index); if (item = = Esritoccontrolitem.esritoccontrolitemlayer) {M_toolmenulayer.additem (new Ow Ntolayer (layer, axMapControl1, Eve), 0, 0, False, ESRI.                    ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText); M_toolmenulayer.additem (New Fullextent (AxMapControl1), 0, 1, false, ESRI.                    ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText); M_toolmenulayer.additem (layer), 0, 2, falsE, ESRI.                    ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText); M_toolmenulayer.additem (layer), 0, 3, False, ESRI.                    ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);                    M_toolmenulayer.popupmenu (e.x, e.y, M_toccontrol.hwnd);                M_toolmenulayer.removeall (); }            }        }

The effect is as follows:

Description:

1.public int AddItem (object item,int subtype,int index,bool begingroup,esricommandstyles Style)

First parameter: The content of the menu item, the function realizes.
Second parameter: When a tool is defined with multiple type, each int represents a new implementation.
The third parameter: The index value, where it appears above the menu item. The default is-1, sorted by writing order.
The fourth parameter: whether to start a new group is to have a "--" effect on it.
Fifth parameter: Display style.

2. Axtoccontrol1.hittest () method

[C #]  void HitTest ( intX, intY, ref esritoccontrolitemItemType, ref IB AsicmapBasicmap, ref ILayerLayer, ref objectUnk, ref objectData); 
Product availabilityavailable with ArcGIS engine.description

x is the x coordinate, in pixels, where the mouse button was pressed referenced against the origin (0, 0) of the Toccontrol (the top left hand corner).

y is the y coordinate, in pixels, where the mouse button was pressed referenced against the origin (0, 0) of the Toccontrol (the top left hand corner).

ItemType Specifies an enumeration indicating the type of item (none, map, layer, heading or legend Class).

Map Specifies an IMap object.

Layer Specifies an ILayer object.

Unk Specifies an Ilegendgroup object.

Data Specifies a long indicating the index of the legend class within the legend group. Use this index in conjunction with the legend group to obtain a particular legend class. An index of-1 refers to the heading if it is present.

Note: The environment used in this article: windows7 operating system; VS2010; based on C # language; Arcengine version is 10.1.

C # Arcengine Toccontrol implementation right-click

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.