zoukankan      html  css  js  c++  java
  • c# 关于Handler得一点说明。

    代码如下:

    private void toolStripButton1_Click(object sender, EventArgs e)
            {
                    this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
                   PointColl[SelectCount] = new List<SharpMap.Geometries.Point>();
                    SelectCount++;
               
            }
            private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
            {
                List<SharpMap.Geometries.Geometry> geometries = new List<SharpMap.Geometries.Geometry>();
                SharpMap.Geometries.Point p = myMap.ImageToWorld(e.Location);
                PointColl[SelectCount-1].Add(p);
                geometries.Add(new SharpMap.Geometries.Polygon(new SharpMap.Geometries.LinearRing(PointColl[SelectCount-1])));//选区
                SharpMap.Layers.VectorLayer layerSelect = new SharpMap.Layers.VectorLayer("Select"+(SelectCount-1).ToString());
                layerSelect.DataSource = new SharpMap.Data.Providers.GeometryProvider(geometries);
                Color Mycolor = Color.FromArgb(128, selectionColor);//说明:1-(128/255)=1-0.5=0.5 透明度为0.5,即50%
                layerSelect.Style.Fill = new SolidBrush(Mycolor);
                layerSelect.SRID = 4326;
                 if (PointColl[SelectCount - 1].Count != 1)
                {
                    VectorLayer deleteLayer = myMap.GetLayerByName("Select" + (SelectCount - 1).ToString()) as VectorLayer;
                    myMap.Layers.Remove(deleteLayer);
                }
                myMap.Layers.Add(layerSelect);
                pictureBox1.Image = myMap.GetMap();
            }

    目的是在新加层的时候把原来的删掉,防止太多造成不透明,但是如果是完整的选层已经做好的话,就保留下来(通过名字来删,这样整的选层的选层就可保留)。
    问题在于在选好一个选第二个时发现pictureBox1_MouseClick函数执行了两次,想了下 ,多点了几次toolStripButton1,多触发了几次toolStripButton1_Click函数,结果发现触发的pictureBox1_MouseClick次数更多了,原来是每次点toolStripButton1都会注册一个this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
    事件,而点鼠标后注册了几个该事件就会触发几次,所以,改称:
            private void toolStripButton1_Click(object sender, EventArgs e)
            {
                try
                {
                    this.pictureBox1.MouseClick -= new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
                    this.pictureBox1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseClick);
                    //this.GotFocus += new EventHandler(WeatherForecast_GotFocus);
                    PointColl[SelectCount] = new List<SharpMap.Geometries.Point>();
                    SelectCount++;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }
    问题解决。

  • 相关阅读:
    Android 最火框架XUtils之注解机制具体解释
    Oracle GoldenGate从oracle db 到非oracle db的初始化数据同步的方法
    Java中接口和抽象类的比較
    spring+springmvc+hibernate架构、maven分模块开发样例小项目案例
    配置Java连接池的两种方式:tomcat方式以及spring方式
    Ant报错之out of memory
    Mybatis 框架文档 超具体笔记
    jsp
    HDU 1251 统计难题(字典树)
    HDU 1251 统计难题(字典树)
  • 原文地址:https://www.cnblogs.com/bnuvincent/p/1551375.html
Copyright © 2011-2022 走看看