这是自定义事件的一个例子,希望可以帮到你:using System; using System.Timers; namespace Ch12Ex02 { /// <summary> /// Summary description for Connection. /// </summary>
public delegate void MessageHandler(string messageText); public class Connection { public event MessageHandler MessageArrived; private Timer pollTimer; public Connection() { pollTimer = new Timer(100); pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage); } public void Connect() { pollTimer.Start(); } public void Disconnect() { pollTimer.Stop(); } private void CheckForMessage(object source, ElapsedEventArgs e) { Console.WriteLine("Checking for new messages."); Random random = new Random(); if ((random.Next(9) == 0) && (MessageArrived != null)) { MessageArrived("Hello Mum!"); } } } }
续: using System;namespace Ch12Ex02 { /// <summary> /// Summary description for Display. /// </summary> public class Display {
public void DisplayMessage(string message) { Console.WriteLine("Message arrived: {0}", message); } } }---------------------------------------------------------------------------------using System;namespace Ch12Ex02 { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { Connection myConnection = new Connection(); Display myDisplay = new Display(); myConnection.MessageArrived += new MessageHandler (myDisplay.DisplayMessage); myConnection.Connect(); Console.ReadLine(); } } }
问题就是:这是我以前的贴子:大家都知道,如果一个控件具有集合属型的属性(比如:ToolBar),那么在编辑该集合类型属性时就会弹出来一个“集合编辑器”对话框。而这个对话框实际就是CollectionEditor类的实现。我的问题有1.我自定义一个控件,在这个控件(MyControl)中有一个属性Items,其类型为MyCollection(继承自CollectionBase),在MyCollection中有一个Add方法,该方法调用List的Add方法,而且还有其他的一些语句。我的问题是,在使用“集合编辑器”新增MyColtrol.Items属性的元素时,好象根本就没有使用MyCollection.Add方法,因此在MyCollection.Add中除了List.Add(***)的语句行外,其他的都没有执行(其中List.Add(***)可能也没有执行,只不过是有这个效果)。但是我非常希望能将MyCollection.Add方法能在使用“集合编辑器”单击新增按钮时执行,这个问题怎么解决? 2.上一个问题无法解决,那我就不指望CollectionEditor了,我就自已定制一个编辑器好了,就像是TreeNode的Editor那样,可是这个里面还是存在一个问题: 我无法继承CollectionEditor里面的CollectionForm类,可是这个类我明明是声明为Protected的呀?如下: Public Class MyCollectionEditor Inherits CollectionEditorPublic Sub New(Byval type as Type) MyBase.New(type) End SubProtected Overrides Function CreateCollectionForm() As CollectionForm Return New MyCollectionForm(Me) End FunctionPrivate Class MyCollectionForm Inherits CollectionForm......End Class...End Class为什么在Private Class MyCollectionForm类中就无法继承CollectionForm呢,提示是:“CollectionForm类是Protected”,可是我就是在CollectionEditor类的子类中使用的呀?为什么在一个类的子类中就不能使用基类中的Protected类型的对象呢?还有想顺便请教一个各位高手,如果我想让自定义控件在设计时就能看到一些效果,比如:在添加ToolBar的ToolBarButton时,就会在设计中的窗体上看到你刚才添加的ToolBarButton了。要达到这个目的该怎么做呢?请各位大哥给小弟支个招:)
/// 显示test属性
/// </summary>
public string test
{
get{return m_test;}
}
using System.Timers;
namespace Ch12Ex02
{
/// <summary>
/// Summary description for Connection.
/// </summary>
public delegate void MessageHandler(string messageText); public class Connection
{
public event MessageHandler MessageArrived; private Timer pollTimer; public Connection()
{
pollTimer = new Timer(100);
pollTimer.Elapsed += new ElapsedEventHandler(CheckForMessage);
} public void Connect()
{
pollTimer.Start();
} public void Disconnect()
{
pollTimer.Stop();
} private void CheckForMessage(object source, ElapsedEventArgs e)
{
Console.WriteLine("Checking for new messages.");
Random random = new Random();
if ((random.Next(9) == 0) && (MessageArrived != null))
{
MessageArrived("Hello Mum!");
}
}
}
}
using System;namespace Ch12Ex02
{
/// <summary>
/// Summary description for Display.
/// </summary>
public class Display
{
public void DisplayMessage(string message)
{
Console.WriteLine("Message arrived: {0}", message);
}
}
}---------------------------------------------------------------------------------using System;namespace Ch12Ex02
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Connection myConnection = new Connection();
Display myDisplay = new Display();
myConnection.MessageArrived +=
new MessageHandler (myDisplay.DisplayMessage);
myConnection.Connect();
Console.ReadLine();
}
}
}
试一试invalidate(new object(),new System.EventArg());
不就可以了么,我调用click时都是这么调用的啊
定义委托也是一个办法,小弟也曾想定义一个Timer然后再定义一个事件,然后在设计时一直检测,如果集合有改变,那么就发出事件。
可是这样真的感觉到很不爽,只能算是一个权宜之计,我之所以想把设计时行为加上,就是想让程序更加完美,如果采用权宜之计还不如不要了。lovelyxinxin(小欣欣)兄:谢谢你的参与,你可能还没有看清楚我的意思,我不是不会发出调用一个事件,而是我不知道在什么地方才能调用这个事件。
public delegate void ModelChangedHandler(Object sender, System.EventArgs args);
声明一个事件
public event ModelChangedHandler OnModelChanged;
private void fireModelChanged()
{
if (OnModelChanged != null)
OnModelChanged(this,EventArgs.Empty);
}当在控件中加入集合属性时,向控件指定听众。
Control.OnModelChanged += new ModelChangedHandler (MyHandler);最后编写处理方法
private void MyHandler(Object sender, System.EventArgs e)
{
//调用invalidate
this.invalidate();
}
不知这样是不是可行,如果可以的话。请告诉我一声
2.上一个问题无法解决,那我就不指望CollectionEditor了,我就自已定制一个编辑器好了,就像是TreeNode的Editor那样,可是这个里面还是存在一个问题:
我无法继承CollectionEditor里面的CollectionForm类,可是这个类我明明是声明为Protected的呀?如下:
Public Class MyCollectionEditor
Inherits CollectionEditorPublic Sub New(Byval type as Type)
MyBase.New(type)
End SubProtected Overrides Function CreateCollectionForm() As CollectionForm
Return New MyCollectionForm(Me)
End FunctionPrivate Class MyCollectionForm
Inherits CollectionForm......End Class...End Class为什么在Private Class MyCollectionForm类中就无法继承CollectionForm呢,提示是:“CollectionForm类是Protected”,可是我就是在CollectionEditor类的子类中使用的呀?为什么在一个类的子类中就不能使用基类中的Protected类型的对象呢?还有想顺便请教一个各位高手,如果我想让自定义控件在设计时就能看到一些效果,比如:在添加ToolBar的ToolBarButton时,就会在设计中的窗体上看到你刚才添加的ToolBarButton了。要达到这个目的该怎么做呢?请各位大哥给小弟支个招:)
Email: [email protected]