关键字查询
1.制作子窗体
2.编写代码
private void frmkeywordSearch_Load(object sender, EventArgs e)
{
try
{
//将当前图层列表清空
comboBox2.Items.Clear();
string layerName; //设置临时变量存储图层名称
//对Map中的每个图层进行判断并加载名称
for (int i = 0; i < currentmap.LayerCount; i++)
{
//如果该图层为图层组类型,则分别对所包含的每个图层进行操作
if (currentmap.get_Layer(i) is GroupLayer)
{
//使用ICompositeLayer接口进行遍历操作
ICompositeLayer compositeLayer = currentmap.get_Layer(i) as ICompositeLayer;
for (int j = 0; j < compositeLayer.Count; j++)
{
//将图层的名称添加到comboBoxLayerName控件中
layerName = compositeLayer.get_Layer(j).Name;
comboBox2.Items.Add(layerName);
}
}
//如果图层不是图层组类型,则直接添加名称
else
{
layerName = currentmap.get_Layer(i).Name;
comboBox2.Items.Add(layerName);
}
}
//将comboBoxLayerName控件的默认选项设置为第一个图层的名称
comboBox2.SelectedIndex = 0;
}
catch { }
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
//首先将字段列表和字段值列表清空
listBox1.Items.Clear();
IField field; //设置临时变量存储使用IField接口的对象
for (int i = 0; i < currentmap.LayerCount; i++)
{
if (currentmap.get_Layer(i) is GroupLayer)
{
ICompositeLayer compositeLayer = currentmap.get_Layer(i) as ICompositeLayer;
for (int j = 0; j < compositeLayer.Count; j++)
{
//判断图层的名称是否与comboBoxLayerName控件中选择的图层名称相同
if (compositeLayer.get_Layer(j).Name == comboBox2.SelectedItem.ToString())
{
//如果相同则设置为整个窗体所使用的IFeatureLayer接口对象
currentFeatureLayer = compositeLayer.get_Layer(j) as IFeatureLayer;
break;
}
}
}
else
{
//判断图层的名称是否与comboBoxLayerName中选择的图层名称相同
if (currentmap.get_Layer(i).Name == comboBox2.SelectedItem.ToString())
{
//如果相同则设置为整个窗体所使用的IFeatureLayer接口对象
currentFeatureLayer = currentmap.get_Layer(i) as IFeatureLayer;
break;
}
}
}
//使用IFeatureClass接口对该图层的所有属性字段进行遍历,并填充listBoxFields控件
for (int i = 0; i < currentFeatureLayer.FeatureClass.Fields.FieldCount; i++)
{
//根据索引值获取图层的字段
field = currentFeatureLayer.FeatureClass.Fields.get_Field(i);
//排除SHAPE字段,并在其它字段名称前后添加字符"和字符"
if (field.Name.ToUpper() != "SHAPE")
{
listBox1.Items.Add(field.Name);
}
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//执行属性查询操作,并关闭窗体
SelectFeaturesByAttribute();
FormSelection formSelection = new FormSelection();
//将当前主窗体中MapControl控件中的Map对象赋值给FormQueryByAttribute窗体的CurrentMap属性
formSelection.CurrentMap = currentmap;
//显示属性查询窗体
formSelection.Show();
}
catch { }
}
private void SelectFeaturesByAttribute()
{
//使用FeatureLayer对象的IFeatureSelection接口来执行查询操作。这里有一个接口转换操作。
IFeatureSelection featureSelection = currentFeatureLayer as IFeatureSelection;
//新建IQueryFilter接口的对象来进行where语句的定义
IQueryFilter queryFilter = new QueryFilterClass();
//设置where语句内容
queryFilter.WhereClause = listBox1.SelectedItem.ToString()+"="+textBox1.Text.Trim();
//通过接口转换使用Map对象的IActiveView接口来部分刷新地图窗口,从而高亮显示查询的结果
IActiveView activeView = currentmap as IActiveView;
//acview = activeView;
//根据查询选择方式的不同,得到不同的选择集
//首先使用IMap接口的ClearSelection()方法清空地图选择集
currentmap.ClearSelection();
//根据定义的where语句使用IFeatureSelection接口的SelectFeatures方法选择要素,并将其添加到选择集中
featureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultNew, false);
//部分刷新操作,只刷新地理选择集的内容
activeView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, activeView.Extent);
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
3.添加的引用
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
4.传递地图参数map到子窗体