自定义用户控件的事件处理
=======================
控件内部 class userControl1
public event anyName() //定义事件对外提供接口
public event anyName EventName;
触发事件:
if(EventName!=null)
EventName;
引用控件的模块
this.userControl11.EventName +=new userControl1(this.userControl1_EventName);
这里的System.eventHandler可以是你自己的类的名字.this.output是你处理事件的过程的名字.
=======================
1. public event EventHandler Change; //定义事件对外提供接口
2. this.Change +=new System.EventHandler(this.outPut); //初始化事件
3. 定义事件函数
private void outPut(object sender,System.EventArgs e)
{
Response.Write("OK");
}
4。调用事件
private void Button1_Click(object sender, System.EventArgs e)
{
this.Change(this,e);
// Response.Write("ASDFASDFSAD");
}
=======================
如果是固定控件,可先在用户控件中声明一个委托。
public event EventHandler SelectedIndexChanged;
private void InitializeComponent()
{
this.TreeView1.SelectedIndexChange += new Microsoft.Web.UI.WebControls.SelectEventHandler(this.TreeView1_SelectedIndexChange);
this.Load += new System.EventHandler(this.Page_Load);
}
private void TreeView1_SelectedIndexChange(object sender, TreeViewSelectEventArgs e)
{
if(SelectedIndexChanged!=null)
{
this.SelectedIndexChanged(this,e);
}
}
============================
呵呵,正是使用delegate啊!
//定义一个你自己的EventArgs,当然,这不是必需的。
public class DatagridEventArgs : EventArgs
{
private int mPage;
public DatagridEventArgs(int page)
{
mPage = page;
}
public int Page
{
get { return mPage; }
set { mPage = value; }
}
}
//然后声明一个delegate:
public delegate void DatagridEventHandler(object sender, DatagridEventArgs e);
//然后定义你的event:
public event DatagridEventHandler DataBinded;
//最后定义你的方法:
protected void OnDataBinded(DatagridEventArgs e)
{
DataBinded(this, e);
}
//在程序代码中这样调用:
DatagridEventArgs e = new DatagridEventArgs(mCurrentPage);
DataBinded(this,e);
=====================================
我现在作了一个控件,急需公开内部事件,怎么作?
//delegate declaration
delegate void MyDelegate();
public class MyClass
{
public void InstanceMethod()
{
Console.WriteLine("A message from the instance method.");
}
static public void StaticMethod()
{
Console.WriteLine("A message from the static method.");
}
}
public class MainClass
{
static public void Main()
{
MyClass p = new MyClass();
//Map the delegate to the instance method:
MyDelegate d = new MyDelegate(p.InstanceMethod);
d();
//Map to the static method:
d = new MyDelegate(MyClass.StaticMethod);
d();
}
}
=======================
如果你的控件不是从Control派生的,则:
public class YourControl : ......
{
public event EventHandler MouseClick;
public event KeyEventHandler KeyDown;
.....
protected void RaiseKeyDown()
{
foreach(EventHandler ev = MouseClick.GetInvocationList() )
ev(this , new EventArgs() );
}
protected void RaiseMouseClick()
{
KeyEventArgs e = new KeyEventArgs();
// initialize e at here
foreach(KeyEventHandler ev = MouseClick.GetInvocationList() )
ev(this , e );
}
....
}
谢了,不过它不能生成MouseClick,KeyDown之类了事件。帮帮忙再想想办法吧,谢了谢了!
=======================
class Client
{
protected YourControl theControl;
public Client()
{
theControl = new YourControl();
theControl.MouseClick += new EventHandler( this.YourControl_OnMouseClick() );
}
...........
private void YourControl_OnMouseClick(object sender , EventArgs e)
{
System.Windows.Forms.MessageBox.Show("You clicked me!");
}
}
如果是从System.Windows.Forms.Control派生则它已继承了Control的所有事件!
=======================
先定义一个公共事件:
public Shadows Event LostFocus(ByVal sender As System.Object, ByVal e As System.EventArgs)
再在TextBox的LostFocus事件中触发该事件就ok了
Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
RaiseEvent LostFocus(sender, e)
End sub
=======================
一个自定义控件,继承至Panel,在自定义控件中动态添加了一些Label控件,现在希望能获取panel的鼠标事件,但怎么也不能引发,估计应该是鼠标点击到Label上了,请问,我怎么能把Label的鼠标事件转到Panel上呢?谢谢
--
添加事件,然后在label的鼠标事件里引发。这样就给了继承的panel控件以对外的接口。
是这样么?楼主试试。
Public Class MyPanel
Inherits Windows.Forms.Panel
Private WithEvents m_Label As Windows.Forms.Label
Public Sub New()
MyBase.New()
Me.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
m_Label = New Windows.Forms.Label
m_Label.Text = "MyLabel"
m_Label.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
Me.Controls.Add(m_Label)
End Sub
Private Sub MyPanel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
MsgBox("panel click")
End Sub
Private Sub m_Label_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles m_Label.Click
MyBase.OnClick(New EventArgs)
End Sub
End Class