Skip to content

Instantly share code, notes, and snippets.

@district10
Last active August 29, 2015 14:07
Show Gist options
  • Save district10/2310f0e4045831a51527 to your computer and use it in GitHub Desktop.
Save district10/2310f0e4045831a51527 to your computer and use it in GitHub Desktop.
Shit like a shit
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
namespace MyGIS.Forms
{
public partial class AttributeQueryForm : Form
{
//地图数据
private AxMapControl mMapControl;
//选中图层
private IFeatureLayer mFeatureLayer;
//修改构造函数,获取MapControl
public AttributeQueryForm(AxMapControl mapControl)
{
InitializeComponent();
this.mMapControl = mapControl;
}
private void AttributeQueryForm_Load(object sender, EventArgs e)
{
//MapControl中没有图层时返回
if (this.mMapControl.LayerCount <= 0)
return;
//获取MapControl中的全部图层名称,并加入ComboBox
//图层
ILayer pLayer;
//图层名称
string strLayerName;
for (int i = 0; i < this.mMapControl.LayerCount; i++)
{
pLayer = this.mMapControl.get_Layer(i);
strLayerName = pLayer.Name;
//图层名称加入cboLayer
this.cboLayer.Items.Add(strLayerName);
}
//默认显示第一个选项
this.cboLayer.SelectedIndex = 0;
}
private void cboLayer_SelectedIndexChanged(object sender, EventArgs e)
{
//获取cboLayer中选中的图层
mFeatureLayer = mMapControl.get_Layer(cboLayer.SelectedIndex) as IFeatureLayer;
IFeatureClass pFeatureClass = mFeatureLayer.FeatureClass;
//字段名称
string strFldName;
for (int i = 0; i < pFeatureClass.Fields.FieldCount;i++ )
{
strFldName = pFeatureClass.Fields.get_Field(i).Name;
//图层名称加入cboField
this.cboField.Items.Add(strFldName);
}
//默认显示第一个选项
this.cboField.SelectedIndex = 3;
}
private void btnOk_Click(object sender, EventArgs e)
{
//查找内容为空时返回
if (txtValue.Text == null)
return;
IQueryFilter pQueryFilter = new QueryFilterClass();
IFeatureCursor pFeatureCursor;
IFeature pFeature;
pQueryFilter.WhereClause = cboField.Text + "='" + txtValue.Text + "'";
pFeatureCursor = mFeatureLayer.Search(pQueryFilter,true);
pFeature = pFeatureCursor.NextFeature();
//判断是否获取到要素
if (pFeature != null)
{
//选择要素
mMapControl.Map.SelectFeature(mFeatureLayer, pFeature);
//放大到要素
mMapControl.Extent = pFeature.Shape.Envelope;
}
else
{
//没有得到pFeature的提示
MessageBox.Show("没有找到" + txtValue.Text + "。", "提示");
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Dispose();
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
namespace MyGIS.Forms
{
public partial class SpatialQueryForm : Form
{
//获取主界面的MapControl对象
private AxMapControl mMapControl;
//查询方式
public int mQueryMode;
//图层索引
public int mLayerIndex;
//为构造函数添加参数MapControl
public SpatialQueryForm(AxMapControl mapControl)
{
InitializeComponent();
this.mMapControl = mapControl;
}
private void SpatialQueryForm_Load(object sender, EventArgs e)
{
//MapControl中没有图层时返回
if (this.mMapControl.LayerCount <= 0)
return;
//获取MapControl中的全部图层名称,并加入ComboBox
//图层
ILayer pLayer;
//图层名称
string strLayerName;
for (int i = 0; i < this.mMapControl.LayerCount; i++)
{
pLayer = this.mMapControl.get_Layer(i);
strLayerName = pLayer.Name;
//图层名称加入ComboBox
this.cboLayer.Items.Add(strLayerName);
}
//加载查询方式
this.cboMode.Items.Add("矩形查询");
this.cboMode.Items.Add("线查询");
this.cboMode.Items.Add("点查询");
this.cboMode.Items.Add("圆查询");
//初始化ComboBox默认值
this.cboLayer.SelectedIndex = 0;
this.cboMode.SelectedIndex = 0;
}
private void btnOk_Click(object sender, EventArgs e)
{
//设置鼠标点击时窗体的结果
this.DialogResult = DialogResult.OK;
//判断是否存在图层
if (this.cboLayer.Items.Count <= 0)
{
MessageBox.Show("当前MapControl没有添加图层!","提示");
return;
}
//获取选中的查询方式和图层索引
this.mLayerIndex = this.cboLayer.SelectedIndex;
this.mQueryMode = this.cboMode.SelectedIndex;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Dispose();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment