如何在自定义控件中触发控件原有的事件:
我做一个简单的小控件,只包含一个picturebox对象picMap,我想在picMap的响应双击事件直接触发自定义控件的双击事件,代码如下,运行是发生错误:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;namespace winPicMap
{
/// <summary>
/// UserControl1 的摘要说明。
/// </summary>
public class UserControl1 : System.Windows.Forms.UserControl
{
private System.Windows.Forms.PictureBox picMap;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null; public UserControl1()
{
// 该调用是 Windows.Forms 窗体设计器所必需的。
InitializeComponent(); // TODO: 在 InitComponent 调用后添加任何初始化 } /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if( components != null )
components.Dispose();
}
base.Dispose( disposing );
} #region 组件设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器 
/// 修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.picMap = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
// 
// picMap
// 
this.picMap.Dock = System.Windows.Forms.DockStyle.Fill;
this.picMap.Location = new System.Drawing.Point(0, 0);
this.picMap.Name = "picMap";
this.picMap.Size = new System.Drawing.Size(224, 232);
this.picMap.TabIndex = 0;
this.picMap.TabStop = false;
this.picMap.DoubleClick += new System.EventHandler(this.picMap_DoubleClick);
// 
// UserControl1
// 
this.Controls.Add(this.picMap);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(224, 232);
this.ResumeLayout(false); }
#endregion public event MouseEventHandler DoubleClick;   
private void picMap_DoubleClick(object sender, System.EventArgs e)
{
this.DoubleClick(sender,e);
}
}
}在生成解决方案时生成了3个错误
(1)C:\Documents and Settings\Administrator\桌面\cshpExersice\winPicMap\UserControl1.cs(73): “winPicMap.UserControl1.DoubleClick”上要求关键字 new,因为它隐藏了继承成员“System.Windows.Forms.Control.DoubleClick”
(2) C:\Documents and Settings\Administrator\桌面\cshpExersice\winPicMap\UserControl1.cs(76): 参数“2” : 无法从“System.EventArgs”转换为“System.Windows.Forms.MouseEventArgs”
(3)C:\Documents and Settings\Administrator\桌面\cshpExersice\winPicMap\UserControl1.cs(76): 委托“System.Windows.Forms.MouseEventHandler”有一些无效参数请问:怎样才能让picMap响应了DoubleClick事件后响应我定义的事件

解决方案 »

  1.   

    直接覆写OnDoubleClick函数,然后加上自己的代码就可以了。
      

  2.   

    楼主的意思是说,让多个控件都能响应你的PictureBox控件引发的双击事件吗?如果是,你可让多个控件预定PictureBox控件的双击事件
    比如说,你还有一个Label控件,名为label1,在Form的Load事件中为label1预定PictureBox控件的双击事件:
    pictureBox1.DoubleClick += new System.EventHandler( label1Method );
    label1Method这个方法的签名得注意了,参数必须为(object sender, System.EventArgs e)返回类型为void
      

  3.   

    public event MouseEventHandler myDoubleClick;//换个名字
    private void picMap_DoubleClick(object sender, System.EventArgs e)
    {
    if (myDoubleClick!=null)
    this.myDoubleClick(sender,null);
    }
      

  4.   


                      public event EventHandler myDoubleClick;
    private void picMap_DoubleClick(object sender, System.EventArgs e)
    {
    if (myDoubleClick!=null)
         this.myDoubleClick(sender,e);
    }