用c#作控件事件
我现在作了一个控件,急需公开内部事件,怎么作?大哥大姐快帮忙呀。 问题点数:30、回复次数:3Top
1 楼zhq2000(方舟)回复于 2002-03-11 23:35:32 得分 0
// 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();
}
}Top
2 楼yuechang(昌昌)回复于 2002-03-12 10:46:45 得分 0
谢了,不过它不能生成MouseClick,KeyDown之类了事件。帮帮忙再想想办法吧,谢了谢了!Top
3 楼zhq2000(方舟)回复于 2002-03-12 20:13:04 得分 30
如果你的控件不是从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 );
}
....
}
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的所有事件!Top




