效果图
源码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Controls;
namespace 图层和属性表字段添加进comBox
{
public partial class Form1 : Form
{
public Form1()
{
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
InitializeComponent();
}
//操作图层
IFeatureLayer featureLayer = null;
//特征类集
IFeatureClass featureClass = null;
//程序开始运行时加载地图文档
private void Form1_Load(object sender, EventArgs e)
{
LoadMapDocument();
}
//自定义加载地图文档
private void LoadMapDocument()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开地图文档";
openFileDialog.Filter = "Map Documents(*.mxd)|*.mxd|Shapfile(*.shp)|*.shp|所有文件(*.*)|*.*";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
String filePath = openFileDialog.FileName;
if (axMapControl1.CheckMxFile(filePath))
{
//在加载地图文档前,先清空图层
axMapControl1.Map.ClearLayers();
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerHourglass;
axMapControl1.LoadMxFile(filePath);
axMapControl1.MousePointer = esriControlsMousePointer.esriPointerDefault;
}
else
{
MessageBox.Show(filePath + "不是有效的地图文档");
}
}
}
//当mapcontrol中的图层数据改变时,将新的图层加到图层列表中
private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
{
//要在下拉列表中添加新数据,先清空之前的数据
comboBox1.Items.Clear();
//一旦mapcontrol中的图层被更新,就将图层名添加到下拉列表框
for (int i = 0; i < axMapControl1.LayerCount; i++)
{
//生成一个图层,并将mapcontrol中的指定图层传入
ILayer layer = axMapControl1.get_Layer(i);
//将图层名称添加进列表中
comboBox1.Items.Add(layer.Name);
}
//下拉列表框默认显示第一项
comboBox1.SelectedIndex = 0;
}
//当ComBox中当前显示的文字改变时,执行选择图层操作,并让Map选中当前combox显示的图层
private void comboBox1_TextChanged(object sender, EventArgs e)
{
//判断combox当前显示是否为空
if (comboBox1.Text != "")
{
//获取combox中当前显示的图层,并传给操作图层
featureLayer = axMapControl1.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;
//将操作图层的图层类传给特征类集
featureClass = featureLayer.FeatureClass;
//选中图层中的某一个元素,此方法缺陷是不能够指定选择属性表中的哪一个行元素
//IFeature feature = featureClass.GetFeature(0);
//axMapControl1.Map.ClearSelection();
//axMapControl1.Map.SelectFeature(featureLayer, feature);
//axMapControl1.Refresh();
//图层属性表中的字段名称
String fieldName;
//清空列表框中已有的字段
comboBox2.Items.Clear();
//将指定图层的属性表中的字段添加到列表框中,属性表在特征类集中获得
for (int i = 0; i < featureClass.Fields.FieldCount; i++)
{
//取得指定属性表中的指定字段的名称
fieldName = featureClass.Fields.get_Field(i).Name;
comboBox2.Items.Add(fieldName);
}
//下拉列表框默认显示第一个选项
comboBox2.SelectedIndex = 0;
}
}
}
}