效果图
此方法的缺陷在于,在用户不知道属性表中的字段和数据的情况下,不能实现查询操作;一次只能查询到一个要素,没办法选择两个或多个要素;一次只能选择一个图层中的一个要素,不能同时选择两个图层中的要素。在以后的学习中将解决此问题。
源码:
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.Geometry;
namespace 通过ComBox查询属性表中的数据
{
public partial class Form1 : Form
{
public Form1()
{
ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop);
InitializeComponent();
}
//选中图层
IFeatureLayer featureLayer;
//当mapcontrol中的图层数据改变时,将所有图层名称加载到列表框中
private void axMapControl1_OnMapReplaced(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMapReplacedEvent e)
{
//清空列表框中已有的图层名称
comboBox1.Items.Clear();
//加载图层名称到列表框
IMap map = axMapControl1.Map;
for (int i = 0; i < map.LayerCount; i++)
{
comboBox1.Items.Add(map.get_Layer(i).Name);
}
//列表框默认显示第一项
comboBox1.SelectedIndex = 0;
}
//当列表框中的文字改变时,在字段列表中列出当前文字所显示的图层的字段
private void comboBox1_TextChanged(object sender, EventArgs e)
{
//获取当前显示的图层
featureLayer = axMapControl1.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;
//获取当前显示图层的特征类到特征类集中
IFeatureClass featureClass = featureLayer.FeatureClass;
//清空字段列表中的内容
comboBox2.Items.Clear();
//字段名称
String fieldName = null;
//遍历图层中的字段,加载到列表中
for (int i = 0; i < featureClass.Fields.FieldCount; i++)
{
fieldName = featureClass.Fields.get_Field(i).Name;
comboBox2.Items.Add(fieldName);
}
//默认显示第一项
comboBox2.SelectedIndex = 0;
}
//点击查询按钮查询要素
private void button1_Click(object sender, EventArgs e)
{
//定义要素游标
IFeatureCursor featureCursor = null;
//查询过滤器
IQueryFilter queryFilter = new QueryFilterClass();
//定义图层
IFeature feature = null;
//点
IPoint point = new PointClass();
//获取范围
IEnvelope envelope = axMapControl1.ActiveView.Extent;
//获取mapcontrol展示范围的中心位置
point.X = envelope.XMin + envelope.Width / 2;
point.Y = envelope.YMin + envelope.Height / 2;
if (axMapControl1.LayerCount <= 0) return;
//获取图层,此句代码在ComBox1_TextChanged执行时已经执行过
//featurelayer = axMapControl1.get_Layer(comboBox1.SelectedIndex) as IFeatureLayer;
//清除上次查询的结果
axMapControl1.Map.ClearSelection();
axMapControl1.ActiveView.Refresh();
//设置查询过滤条件,即查询语句
queryFilter.WhereClause = comboBox2.Text + "=" + textBox1.Text;
//使用要素游标获取图层查询的结果
featureCursor = featureLayer.Search(queryFilter, true);
//使用feature获取查询到的要素
feature = featureCursor.NextFeature();
//判断查询到的要素是否为空
if (feature != null)
{
//选择要素
axMapControl1.Map.SelectFeature(featureLayer, feature);
//放大到要素
feature.Shape.Envelope.CenterAt(point);
axMapControl1.Extent = feature.Shape.Envelope;
}
else
{
//没有得到feature的提示
MessageBox.Show("没有找到相关要素!", "提示");
}
}
}
}
此方法的缺陷在于,在用户不知道属性表中的字段和数据的情况下,不能实现查询操作;一次只能查询到一个要素,没办法选择两个或多个要素;一次只能选择一个图层中的一个要素,不能同时选择两个图层中的要素。在以后的学习中将解决此问题。