麻辣GIS微信平台

更多 GIS 干货

微信关注不错过

ArcGIS Engine 10 开发手册(3-7)TOCContro控件

TOCControl 控件使用的是用伙伴控件中的数据地图,它控制图层是否在伙伴控件空显示以及和伙伴控 件在符号上保持一致,TOCControl 为用户提供了一个交互式的环境,如果 TOCControl 控件的伙伴控件是 MapControl 控件,当我们将 TOCControl 控件中图层删掉是,MapControl 控件中相应的图层也会被删掉。

显示属性表的信息

我们知道ArcMap中的Table of Contents 有很多功能,如下图 :

而 ArcGIS Engine 提供的 TOCControl 控件几乎没有提供,那么这些都是需要自己开发的,在这里我做一个 显示属性表的功能。

功能分析

要显示某一个图层的属性表,首先要将这个图层选中,然后在另外一个 Form 中将选中的这个图层 的属性信息进行显示。

添加一个上下文菜单,添加一个新的 Form 窗体,在这个新的窗体上添加 GridView 控件,并在 TOCControl 控件的 OnMouseDown 事件下添加如下代码(pGlobalFeatureLayer 是我定义的一个全局变量):

private void axTOCControl1_OnMouseDown (object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e) {
  if (axMapControl1.LayerCount > 0) {
    esriTOCControlItem pItem = new esriTOCControlItem ();
    pGlobalFeatureLayer = new FeatureLayerClass ();
    IBasicMap pBasicMap = new MapClass ();
    object pOther = new object ();
    object pIndex = new object ();
    axTOCControl1.HitTest (e.x, e.y, ref pItem, ref pBasicMap, ref pGlobalFeatureLayer, ref pOther, ref pIndex);
  }
  if (e.button == 2)
  {
    context.Show (axTOCControl1, e.x, e.y);
  }
}

在上下文菜单的打开属性表的 Click 事件中添加如下代码:

private void ToolStripMenuItem_Click (object sender, EventArgs e)
{
  FormTable Ft = new FormTable (pGlobalFeatureLayer as IFeatureLayer);
  Ft.Show ();
}

在新的窗体中添加一个将属性表显示到 GridView 控件中的函数,如下:

public void Itable2Dtable ()
{
  IFields pFields;
  pFields = pFeatureLayer.FeatureClass.Fields;
  dtGridView.ColumnCount = pFields.FieldCount;
  for (int i = 0; i < pFields.FieldCount; i++)
  {
    string fldName = pFields.get_Field (i).Name;
    dtGridView.Columns[i].Name = fldName;
    dtGridView.Columns[i].ValueType = System.Type.GetType (ParseFieldType (pFields.get_Field (i).Type));
  }

  IFeatureCursor pFeatureCursor;
  pFeatureCursor = pFeatureLayer.FeatureClass.Search (null, false);
  IFeature pFeature;

  pFeature = pFeatureCursor.NextFeature ();
  while (pFeature != null)
  {
    string[] fldValue = new string[pFields.FieldCount];
    for (int i = 0; i < pFields.FieldCount; i++)
    {
      string fldName;
      fldName = pFields.get_Field (i).Name;
      if (fldName == pFeatureLayer.FeatureClass.ShapeFieldName)
      {
        fldValue[i] = Convert.ToString (pFeature.Shape.GeometryType);
      } else
        fldValue[i] = Convert.ToString (pFeature.get_Value (i));
    }
    dtGridView.Rows.Add (fldValue);
    pFeature = pFeatureCursor.NextFeature ();
  }
}

运行结果

运行后,效果如下:

ArcGIS Engine 10 开发手册全集

ArcGIS Engine 10 开发手册全集: ArcGIS Engine 10 开发手册

相关阅读

麻辣GIS-Sailor

作者:

GIS爱好者,学GIS,更爱玩GIS。

声明

1.本文所分享的所有需要用户下载使用的内容(包括但不限于软件、数据、图片)来自于网络或者麻辣GIS粉丝自行分享,版权归该下载资源的合法拥有者所有,如有侵权请第一时间联系本站删除。

2.下载内容仅限个人学习使用,请切勿用作商用等其他用途,否则后果自负。

手机阅读
公众号关注
知识星球
手机阅读
麻辣GIS微信公众号关注
最新GIS干货
关注麻辣GIS知识星球
私享圈子

已有 4 条评论

  1. 麻辣GIS-拉可可
    1#
    拉可可  · 2018-11-12 15:48

    自定义的全局变量pGlobalFeatureLayer是null,传到tableform里会报错

      1. 麻辣GIS-GIS白
        GIS白  · 2022-06-02 18:00

        FormTable Ft = new FormTable (pGlobalFeatureLayer as IFeatureLayer);这里还可以有参数吗?为什么我这里不可以

      2. 麻辣GIS-没懂诶
        没懂诶  · 2022-06-02 18:02

        axTOCControl1.HitTest (e.x, e.y, ref pItem, ref pBasicMap, ref pGlobalFeatureLayer, ref pOther, ref pIndex);这里报错了,显示无法将Carto.IFeatureLayer隐式转换为ILayer

  2. 麻辣GIS-GIS白
    2#
    GIS白  · 2022-06-02 17:58

    ParseFieldType是个函数吗,是自定义的吗?

留言板(小编看到第一时间回复) 取消回复