首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

求指出异常。被狠批了

2014-01-08 
求指出错误。被狠批了。为什么字段选择的时候,会有多个重复的选项?我的数据没问题的。我保证。public partial

求指出错误。被狠批了。
为什么字段选择的时候,会有多个重复的选项?我的数据没问题的。我保证。

public partial class AttributeQueryForm : Form
    {
        //地图数据
        private AxMapControl mMapControl;
        //选中图层
        private IFeatureLayer mFeatureLayer;

        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 = 0;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //定义图层,要素游标,查询过滤器,要素
            IFeatureLayer pFeatureLayer;
            IFeatureCursor pFeatureCursor;
            IQueryFilter pQueryFilter;
            IFeature pFeature;
            //获取图层
            pFeatureLayer = this.mMapControl.Map.get_Layer(cboLayer.SelectedIndex) as IFeatureLayer;
            IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;




            string strFldName;
            //int i = cboField.SelectedIndex;
            strFldName = pFeatureClass.Fields.get_Field(cboField.SelectedIndex).Name;
            //如果图层名称不是states,程序退出
            //if (pFeatureLayer.Name != "cboLayer.Text")
            //    return;
            //清除上次查询结果
            this.mMapControl.Map.ClearSelection();


            //pQueryFilter的实例化
            pQueryFilter = new QueryFilterClass();
            //设置查询过滤条件
            pQueryFilter.WhereClause = txtStateName.Text; ////"strFldName='" + txtStateName.Text + "'";
            //查询
            pFeatureCursor = pFeatureLayer.Search(pQueryFilter, false);
            //获取查询到的要素
            pFeature = pFeatureCursor.NextFeature();


            //判断是否获取到要素
            if (pFeature != null)
            {
                //选择要素
                this.mMapControl.Map.SelectFeature(pFeatureLayer, pFeature);
                //放大到要素
                this.mMapControl.Extent = pFeature.Shape.Envelope;
            }
            else
            {
                //没有得到pFeature的提示
                MessageBox.Show("没有找到名为" + txtStateName.Text + "的设施", "提示");
            }
            mMapControl.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
        }
    }


求指出异常。被狠批了
[解决办法]
 private void cboLayer_SelectedIndexChanged(object sender, EventArgs e)
        {
           this.cboField.Items.Clear();<---这里要加这句,要不然你每次Change 会保留上次的字段
            //获取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 = 0;
        }

热点排行