FrmQuery.cs
上传用户:xianghe012
上传日期:2022-07-02
资源大小:77k
文件大小:5k
源码类别:

GIS编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using ESRI.ArcGIS.esriSystem;
  9. using ESRI.ArcGIS.SystemUI;
  10. using ESRI.ArcGIS.Geometry;
  11. using ESRI.ArcGIS.Display;
  12. using ESRI.ArcGIS.Geodatabase;
  13. using ESRI.ArcGIS.Carto;
  14. using ESRI.ArcGIS.Controls;
  15. namespace ArcMap
  16. {
  17.     public partial class FrmQuery : Form
  18.     {
  19.         public IMapControl2 pMapControl;
  20.         public IMap pMap;
  21.         public int iLayerIndex;
  22.         public int iFieldIndex;
  23.         public FrmQuery(IMapControl2 pFMapControl)
  24.         {
  25.             InitializeComponent();
  26.             pMapControl = pFMapControl;
  27.             pMap = pFMapControl.Map;
  28.         }
  29.  
  30.         private void FrmQuery_Load(object sender, EventArgs e)
  31.         {                      
  32.             ILayer pLayer;
  33.             for (int i = 0; i < pMap.LayerCount; i++)
  34.             {
  35.                 pLayer = pMap.get_Layer(i);
  36.                 cmbLayers.Items.Add(pLayer.Name);
  37.             }
  38.         }
  39.         private void cmbLayers_TextChanged(object sender, EventArgs e)
  40.         {
  41.             iLayerIndex = cmbLayers.Items.IndexOf(cmbLayers.Text);
  42.             cmbFields.Items.Clear();
  43.         }
  44.         //获得图层的字段名称
  45.         private void cmbFields_DropDown(object sender, EventArgs e)
  46.         {
  47.             cmbFields.Items.Clear();
  48.             IFeatureLayer pFeatureLayer;
  49.             pFeatureLayer = (IFeatureLayer)pMap.get_Layer(iLayerIndex);
  50.             IFields pFields;
  51.             pFields = pFeatureLayer.FeatureClass.Fields;
  52.             for (int i = 0; i < pFields.FieldCount; i++)
  53.             {
  54.                 string fieldName;
  55.                 fieldName = pFields.get_Field(i).Name;
  56.                 cmbFields.Items.Add(fieldName);
  57.             }
  58.         }
  59.         private void btnShowAllValue_Click(object sender, EventArgs e)
  60.         {
  61.             if (cmbFields.Text=="")
  62.             {
  63.                 MessageBox.Show("请选择字段名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  64.                 return;
  65.             }
  66.             listBoxValue.Items.Clear();
  67.             IFeatureLayer pFeatureLayer;
  68.             pFeatureLayer = (IFeatureLayer)pMap.get_Layer(iLayerIndex);
  69.             IFeatureCursor pFeatureCursor;
  70.             pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false);
  71.             IFeature pFeature;
  72.             pFeature = pFeatureCursor.NextFeature();
  73.             while(pFeature!=null)
  74.             {
  75.                 string fldValue;
  76.                 if (cmbFields.Text == "Shape")
  77.                 {
  78.                     fldValue = Convert.ToString(pFeature.Shape.GeometryType);
  79.                 }
  80.                 else
  81.                     fldValue = Convert.ToString(pFeature.get_Value(iFieldIndex));             
  82.                 
  83.                 listBoxValue.Items.Add(fldValue);
  84.                 pFeature = pFeatureCursor.NextFeature();
  85.             }
  86.         }
  87.         private void cmbFields_TextChanged(object sender, EventArgs e)
  88.         {
  89.             iFieldIndex = cmbFields.Items.IndexOf(cmbFields.Text);
  90.         }
  91.         private void listBoxValue_DoubleClick(object sender, EventArgs e)
  92.         {
  93.             txtValue.Text = Convert.ToString(listBoxValue.SelectedItem);
  94.         }
  95.         private void btnCancle_Click(object sender, EventArgs e)
  96.         {
  97.            // this.close();
  98.         }
  99.         private void btnQuery_Click(object sender, EventArgs e)
  100.         {
  101.             IActiveView pActiveView;
  102.             pActiveView = (IActiveView)pMap;
  103.             pMap.ClearSelection();
  104.             pActiveView.Refresh();
  105.             IQueryFilter pQueryFilter = new QueryFilterClass();         
  106.             IFeatureLayer pFeatureLayer;
  107.             pFeatureLayer = (IFeatureLayer)pMap.get_Layer(iLayerIndex);
  108.             IFields pFields;
  109.             pFields = pFeatureLayer.FeatureClass.Fields;
  110.             IField pField;
  111.             pField=pFields.get_Field(iFieldIndex);
  112.             switch(pField.Type)
  113.             {
  114.                 case esriFieldType.esriFieldTypeString:
  115.                     pQueryFilter.WhereClause=cmbFields.Text + " = '" + txtValue.Text + "'";
  116.                     break;
  117.                 case esriFieldType.esriFieldTypeDouble:
  118.                 case esriFieldType.esriFieldTypeInteger:
  119.                 case esriFieldType.esriFieldTypeSingle:
  120.                 case esriFieldType.esriFieldTypeSmallInteger:
  121.                     pQueryFilter.WhereClause = cmbFields.Text + " = " + txtValue.Text ;
  122.                     break;
  123.             }
  124.             IFeatureCursor pFeatureCursor;
  125.             pFeatureCursor = pFeatureLayer.FeatureClass.Search(pQueryFilter, false);
  126.             IFeature pFeature;
  127.             pFeature = pFeatureCursor.NextFeature();
  128.             while (pFeature!=null)
  129.             {
  130.                 pMap.SelectFeature(pFeatureLayer, pFeature);
  131.                 pFeature = pFeatureCursor.NextFeature();
  132.             }
  133.             pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
  134.         }
  135.         private void btnTable_Click(object sender, EventArgs e)
  136.         {
  137.             frmTable fTable = new frmTable(pMapControl, cmbLayers.Text);
  138.             fTable.Show();
  139.         }
  140.  
  141.     }
  142. }