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());
            }
        }
问题解决。

C#中,一个句柄(handler)通常指代一个用于访问和操作底层资源的对象或数据结构。句柄可以是指针、引用或其他形式的标识符。 在C#中,可以使用句柄来访问和操作诸如文件、网络连接、数据库连接、窗口句柄等底层资源。通过使用句柄,可以将底层资源的操作封装在一个高级的、易于使用的接口中,从而简化开发过程。 例如,当你在C#中处理文件时,可以使用文件句柄来打开、读取、写入和关闭文件。句柄允许你直接与文件系统进行交互,而无需关心底层的实现细节。 以下是一个使用文件句柄的简单示例: ```csharp using System; using System.IO; class Program { static void Main() { // 打开文件并获取文件句柄 FileStream file = File.Open("example.txt", FileMode.Open); // 读取文件内容 byte[] buffer = new byte[1024]; int bytesRead = file.Read(buffer, 0, buffer.Length); string content = System.Text.Encoding.UTF8.GetString(buffer, 0, bytesRead); Console.WriteLine(content); // 关闭文件句柄 file.Close(); } } ``` 在上面的示例中,`File.Open` 方法用于打开一个文件并返回一个文件句柄(`FileStream`)。然后,我们可以使用该句柄(`file`)来读取文件的内容,并最后关闭文件句柄。 需要注意的是,使用句柄时应当遵循适当的资源管理和错误处理,以确保正确释放和处理底层资源,避免资源泄漏和潜在的异常情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值