📄 form_query.cs
字号:
using System;
using System.Threading;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using ESRI.ArcGIS.MapControl;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Geodatabase;
namespace Cstest1.WindowsForm
{
/// <summary>
/// Form_Query 的摘要说明。
/// </summary>
public class Form_Query : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label_Find;
private System.Windows.Forms.ComboBox comboBox_FeatureName;
private System.Windows.Forms.Label label_In;
private System.Windows.Forms.ComboBox comboBox_TargetLayer;
private System.Windows.Forms.Label label_Field;
private System.Windows.Forms.ComboBox comboBox_Field;
private System.Windows.Forms.Button button_Find;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public ESRI.ArcGIS.MapControl.AxMapControl m_MapControl;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.ListBox listBox_Result;
private System.Windows.Forms.Button button1;
const esriViewDrawPhase Phase=ESRI.ArcGIS.Carto.esriViewDrawPhase.esriViewGeoSelection; //刷新要素选择集(常量)
IFeatureCursor m_FeatureCursor; //选择集的游标
private bool result; //查找结果(是否找到要素)
public Form_Query(ESRI.ArcGIS.MapControl.AxMapControl m_MainMap)
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
m_MapControl=m_MainMap;
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form_Query));
this.label_Find = new System.Windows.Forms.Label();
this.comboBox_FeatureName = new System.Windows.Forms.ComboBox();
this.label_In = new System.Windows.Forms.Label();
this.comboBox_TargetLayer = new System.Windows.Forms.ComboBox();
this.label_Field = new System.Windows.Forms.Label();
this.comboBox_Field = new System.Windows.Forms.ComboBox();
this.button_Find = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.listBox_Result = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// label_Find
//
this.label_Find.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label_Find.Location = new System.Drawing.Point(8, 16);
this.label_Find.Name = "label_Find";
this.label_Find.Size = new System.Drawing.Size(40, 24);
this.label_Find.TabIndex = 1;
this.label_Find.Text = "查找";
//
// comboBox_FeatureName
//
this.comboBox_FeatureName.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.comboBox_FeatureName.Location = new System.Drawing.Point(72, 16);
this.comboBox_FeatureName.Name = "comboBox_FeatureName";
this.comboBox_FeatureName.Size = new System.Drawing.Size(160, 22);
this.comboBox_FeatureName.TabIndex = 2;
this.comboBox_FeatureName.Text = "目标要素";
this.comboBox_FeatureName.MouseDown += new System.Windows.Forms.MouseEventHandler(this.comboBox_FeatureName_MouseDown);
this.comboBox_FeatureName.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.comboBox_FeatureName_KeyPress);
this.comboBox_FeatureName.SelectedIndexChanged += new System.EventHandler(this.comboBox_FeatureName_SelectedIndexChanged);
//
// label_In
//
this.label_In.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label_In.Location = new System.Drawing.Point(8, 48);
this.label_In.Name = "label_In";
this.label_In.Size = new System.Drawing.Size(56, 16);
this.label_In.TabIndex = 3;
this.label_In.Text = "在图层";
//
// comboBox_TargetLayer
//
this.comboBox_TargetLayer.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox_TargetLayer.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.comboBox_TargetLayer.Items.AddRange(new object[] {
"所有图层"});
this.comboBox_TargetLayer.Location = new System.Drawing.Point(72, 48);
this.comboBox_TargetLayer.Name = "comboBox_TargetLayer";
this.comboBox_TargetLayer.Size = new System.Drawing.Size(160, 22);
this.comboBox_TargetLayer.TabIndex = 4;
this.comboBox_TargetLayer.SelectedIndexChanged += new System.EventHandler(this.comboBox_TargetLayer_SelectedIndexChanged);
//
// label_Field
//
this.label_Field.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label_Field.Location = new System.Drawing.Point(8, 80);
this.label_Field.Name = "label_Field";
this.label_Field.Size = new System.Drawing.Size(56, 16);
this.label_Field.TabIndex = 5;
this.label_Field.Text = "在字段";
//
// comboBox_Field
//
this.comboBox_Field.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox_Field.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.comboBox_Field.Items.AddRange(new object[] {
"所有字段"});
this.comboBox_Field.Location = new System.Drawing.Point(72, 80);
this.comboBox_Field.Name = "comboBox_Field";
this.comboBox_Field.Size = new System.Drawing.Size(160, 22);
this.comboBox_Field.TabIndex = 6;
//
// button_Find
//
this.button_Find.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button_Find.Image = ((System.Drawing.Image)(resources.GetObject("button_Find.Image")));
this.button_Find.ImageAlign = System.Drawing.ContentAlignment.TopLeft;
this.button_Find.Location = new System.Drawing.Point(248, 16);
this.button_Find.Name = "button_Find";
this.button_Find.Size = new System.Drawing.Size(80, 32);
this.button_Find.TabIndex = 7;
this.button_Find.Text = "查找";
this.button_Find.Click += new System.EventHandler(this.button_Find_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.listBox_Result);
this.groupBox1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.groupBox1.Location = new System.Drawing.Point(8, 104);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(328, 128);
this.groupBox1.TabIndex = 10;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "查找结果";
//
// listBox_Result
//
this.listBox_Result.ItemHeight = 14;
this.listBox_Result.Location = new System.Drawing.Point(8, 24);
this.listBox_Result.Name = "listBox_Result";
this.listBox_Result.Size = new System.Drawing.Size(312, 88);
this.listBox_Result.TabIndex = 9;
this.listBox_Result.MouseDown += new System.Windows.Forms.MouseEventHandler(this.listBox_Result_MouseDown);
//
// button1
//
this.button1.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.button1.Location = new System.Drawing.Point(248, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(80, 32);
this.button1.TabIndex = 11;
this.button1.Text = "取消";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form_Query
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(344, 238);
this.Controls.Add(this.button1);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button_Find);
this.Controls.Add(this.comboBox_Field);
this.Controls.Add(this.label_Field);
this.Controls.Add(this.comboBox_TargetLayer);
this.Controls.Add(this.label_In);
this.Controls.Add(this.comboBox_FeatureName);
this.Controls.Add(this.label_Find);
this.Name = "Form_Query";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "属性查询";
this.TopMost = true;
this.Load += new System.EventHandler(this.Form_Query_Load);
this.groupBox1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private void button_Find_Click(object sender, System.EventArgs e) //查找按钮事件
{
result=false; //初始化为false,没找到要素状态
GetSQLString(); //生成SQl语句
if(result==true)
{
for(int i =0; i < comboBox_FeatureName.Items.Count;i++) //判断要添加的关键词是否已存在
{
if(comboBox_FeatureName.Items[i].ToString() == comboBox_FeatureName.Text)
return;
}
comboBox_FeatureName.Items.Add(comboBox_FeatureName.Text); //查找成功则将关键词添加到items
}
else
{
System.Windows.Forms.MessageBox.Show("没有找到指定要素!"); //查找失败则提醒
}
}
private void GetSQLString() //生成SQl语句
{
if(comboBox_FeatureName.Text=="")
{
System.Windows.Forms.MessageBox.Show("请先输入查询关键词!");
comboBox_FeatureName.Focus();
comboBox_FeatureName.SelectAll();
return;
}
m_MapControl.Map.ClearSelection();
m_MapControl.ActiveView.Refresh();
IFeatureLayer m_FeatureLayer;
IFeatureClass m_FeatureClass;
string SqlFilter;
int FieldIndex;
if(comboBox_TargetLayer.SelectedIndex==0) //查找所有图层
{
for(int i=0;i<=m_MapControl.Map.LayerCount - 1;i++)
{
m_FeatureLayer=(IFeatureLayer)m_MapControl.Map.get_Layer(i);
m_FeatureClass=m_FeatureLayer.FeatureClass;
if(comboBox_Field.Text=="所有字段") //查找所有字段
{
for(int j=1;j<=m_FeatureClass.Fields.FieldCount - 1;j++)
{
if(m_FeatureClass.Fields.get_Field(j).Type
==ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString) //对string型字段搜索
{
SqlFilter=m_FeatureClass.Fields.get_Field(j).Name + " Like '%"
+ comboBox_FeatureName.Text.ToString() + "%'";
FindFeature(i,SqlFilter);
}
}
}
else //查找某一字段
{
FieldIndex=m_FeatureClass.Fields.FindField(comboBox_Field.Text);
if(FieldIndex==-1) continue;
if(m_FeatureClass.Fields.get_Field(FieldIndex).Type
==ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString) //对string型字段搜索
{
SqlFilter=m_FeatureClass.Fields.get_Field(FieldIndex).Name + " Like '%"
+ comboBox_FeatureName.Text.ToString() + "%'";
FindFeature(i,SqlFilter);
}
if(m_FeatureClass.Fields.get_Field(FieldIndex).Type
!=ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString)
{
System.Windows.Forms.MessageBox.Show("暂时不能对非字符类型字段进行查找···");
return;
}
}
}
}
else //查找某一图层
{
m_FeatureLayer=(IFeatureLayer)m_MapControl.Map.get_Layer(comboBox_TargetLayer.SelectedIndex - 1);
m_FeatureClass=m_FeatureLayer.FeatureClass;
if(comboBox_Field.Text=="所有字段") //查找所有字段
{
for(int j=1;j<=comboBox_Field.Items.Count - 1;j++)
{
// if(m_FeatureClass.Fields.get_Field(j).Type==
// ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeOID)
// continue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -