如何在自定义控件中触发控件原有的事件:
我做一个简单的小控件,只包含一个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事件后响应我定义的事件
我做一个简单的小控件,只包含一个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事件后响应我定义的事件
解决方案 »
- 怎么解决主窗体线程关闭时子线程却还在执行任务这个问题
- C#:用户编辑数据时,当用户改动了表单数据后退出时如何提示用户保存?
- 关于回帖送分的问题
- 怎样优化这段代码,让它的执行速度更快些?
- 关于DateTime问题
- 参数传递问题,
- 新增问题!!!急用!!!!!!
- Petzold的《Programming Windows With C#》一书中Checker.cs示例程序调用基类的OnResize函数的次序问题
- remoting设置超时实例,谁给一个?要C# winform的
- 如何在更新数据库时更新带有图片类型的字段,焦急等待中。
- 请问如何将一个窗口置于另一个窗口之上
- log4net的配置文件问题!!
比如说,你还有一个Label控件,名为label1,在Form的Load事件中为label1预定PictureBox控件的双击事件:
pictureBox1.DoubleClick += new System.EventHandler( label1Method );
label1Method这个方法的签名得注意了,参数必须为(object sender, System.EventArgs e)返回类型为void
private void picMap_DoubleClick(object sender, System.EventArgs e)
{
if (myDoubleClick!=null)
this.myDoubleClick(sender,null);
}
public event EventHandler myDoubleClick;
private void picMap_DoubleClick(object sender, System.EventArgs e)
{
if (myDoubleClick!=null)
this.myDoubleClick(sender,e);
}