using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
namespace yanShi
{
public partial class frmSelectByAttribute : Form
{
// 声明一个IFeatureLayer类型的变量mFeatureLayer,并初始化为null。IFeatureLayer通常用于表示地图上的一个要素图层。
IFeatureLayer mFeatureLayer = null;
// 声明一个IFeatureClass类型的变量mFeatureClass,并初始化为null。IFeatureClass是ArcGIS Engine中表示要素类的接口,它定义了一个具有相同几何类型和属性集的一组要素。
IFeatureClass mFeatureClass = null;
// 声明一个整型变量isString,用来表示字段是否为字符串类型。
// 0为非字符串
// 1为字符串
int isString = 0;
// 声明一个字符串变量whereClause,用于存储SQL查询语句的条件部分。
string whereClause;
public string WhereClause
{
get { return whereClause; }
set { whereClause = value; }
}
public frmSelectByAttribute()
{
InitializeComponent();
}
// frmSelectByAttribute类的构造函数,它接受一个IFeatureLayer类型的参数fFeatureLayer。
public frmSelectByAttribute(IFeatureLayer fFeatureLayer)
{
// 调用窗体或控件的初始化方法,该方法通常在窗体设计器中自动生成,用于初始化窗体上的控件等。
InitializeComponent();
// 将传入的IFeatureLayer对象赋值给类的成员变量mFeatureLayer。
mFeatureLayer = fFeatureLayer;
// 从mFeatureLayer对象中获取其关联的IFeatureClass对象,并赋值给类的成员变量mFeatureClass。
mFeatureClass = mFeatureLayer.FeatureClass;
// 获取mFeatureClass对象的IFields接口,该接口包含了要素类中所有字段的信息。
IFields pFields = mFeatureClass.Fields;
// 清除下拉列表框cbxField中的所有项,以确保不会显示旧的字段信息。
cbxField.Items.Clear();
// 遍历pFields中的所有字段。
for (int i = 0; i < pFields.FieldCount; i++)
{
// 获取索引为i的字段对象。
IField pField = pFields.get_Field(i);
// 将字段的名称添加到下拉列表框cbxField的项集合中。
cbxField.Items.Add(pField.Name);
}
}
private void cbxField_SelectedIndexChanged(object sender, EventArgs e)
{
// 获取用户从下拉列表框cbxField中选择的字段的索引
int fieldIndex = cbxField.SelectedIndex;
// 创建一个新的字符串列表values,用于存储字段的所有值
List<string> values = new List<string>();
// 创建一个新的字符串列表uniqueValues,用于存储字段的唯一值
List<string> uniqueValues = new List<string>();
// 创建一个新的查询过滤器对象pQueryFilter,并初始化为QueryFilterClass的实例
IQueryFilter pQueryFilter = new QueryFilterClass();
// 初始化查询过滤器的WhereClause属性为空字符串,表示不进行任何过滤
pQueryFilter.WhereClause = "";
// 执行查询,获取IFeatureCursor对象,用于遍历FeatureClass中的要素
// 使用FeatureClass的Search方法,并传入查询过滤器和false(表示不保留对FeatureClass的引用)
IFeatureCursor pFeatureCursor = mFeatureClass.Search(pQueryFilter, false);
// 尝试从FeatureCursor中获取第一个Feature
IFeature pFeature = pFeatureCursor.NextFeature();
// 检查获取到的Feature的指定字段的值是否为字符串类型
if (pFeature.get_Value(fieldIndex).GetType().Name == "String")
{
// 如果是字符串类型,则设置isString为1,可能用于后续的逻辑判断
isString = 1;
}
// 使用while循环遍历FeatureCursor中的所有Feature
while (pFeature != null)
{
// 将Feature的指定字段的值添加到values列表中
values.Add(pFeature.get_Value(fieldIndex).ToString());
// 移动到下一个Feature
pFeature = pFeatureCursor.NextFeature();
}
// 使用LINQ查询从values列表中获取唯一值,并将结果存储在uniqueValues列表中
uniqueValues = values.Distinct().ToList<string>();
// 清除另一个下拉列表框cbxUniqueValue中的所有项
cbxUniqueValue.Items.Clear();
// 遍历uniqueValues列表,并将每个唯一值添加到cbxUniqueValue的下拉列表中
foreach (string temp in uniqueValues)
{
cbxUniqueValue.Items.Add(temp);
}
// 在文本框tbxWhere中追加用户所选字段的双引号包裹的字段名
// 注意:这里只是添加了字段名,并没有构建完整的SQL查询语句
tbxWhere.Text += "\"" + cbxField.SelectedItem.ToString() + "\"";
}
private void cbxUniqueValue_SelectedIndexChanged(object sender, EventArgs e)
{
// 字段值为字符串在Where条件子句中表达方式
if (isString == 1) tbxWhere.Text += "\'" + cbxUniqueValue.SelectedItem.ToString() + "\'";
// 字段值非字符串在Where条件子句中表达方式
if (isString == 0) tbxWhere.Text += cbxUniqueValue.SelectedItem.ToString();
}
private void btnOK_Click(object sender, EventArgs e)
{
whereClause = tbxWhere.Text;
this.Close();
}
private void tbxWhere_TextChanged(object sender, EventArgs e)
{
whereClause = tbxWhere.Text;
}
private void btnClear_Click(object sender, EventArgs e)
{
tbxWhere.Clear();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
————————————————
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/Chckx/article/details/139812833