Dim WithEvents curDwg1 As Database = Application.DocumentManager.MdiActiveDocument.Database Private Sub curDwg1_ObjectAppended(sender As Object, e As ObjectEventArgs) Handles curDwg1.ObjectAppended CreatePaletteSet() Dim NewNode As System.Windows.Forms.TreeNode = MyPalette.TreeView1.Nodes.Add(e.DBObject.GetType().ToString()) NewNode.Tag = e.DBObject.ObjectId.ToString() End Sub
Private Sub AddHanddle() Dim curDwg As Database = Application.DocumentManager.MdiActiveDocument.Database AddHandler curDwg.ObjectAppended, New ObjectEventHandler(AddressOf callback_ObjectAppended) End Sub Private Sub callback_ObjectAppended(ByVal sender As Object, ByVal e As ObjectEventArgs) Dim NewNode As System.Windows.Forms.TreeNode = MyPalette.TreeView1.Nodes.Add(e.DBObject.GetType().ToString()) NewNode.Tag = e.DBObject.ObjectId.ToString() End Sub
above codes do the same setting
so delegate's advantages to use it , i think the event is better than delegate in this method
any explain !!!!
The comparison between AutoCAD event vs delegate can be narrow down to the following code:
1) db.ObjectAppended += (sender, args) => { };
2) db.ObjectAppended += delegate(object sender, ObjectEventArgs args) { };
3) db.ObjectAppended += new ObjectEventHandler(DBOnObjectAppended);
4) db.ObjectAppended += DBOnObjectAppended;
My explanation to simplify the concept:
1. Lambda expression which is a shorter form of delegate, an anonymous delegate.
2. Create a new delegate, which is a template function, with parameters but without implementation. So we have to put our code between { } for implementation.
3. Create a new delegate object ObjectEventHandler, with passing parameter is a real function. Here is an implementation:
public delegate void ObjectEventHandler(object sender, ObjectEventArgs e);
db.ObjectAppended += new ObjectEventHandler(DBOnObjectAppended);
private static void DBOnObjectAppended(object sender, ObjectEventArgs objectEventArgs)
{
}
Event handler is just a delegate type, which represents any function with the same parameters but different implementation. The way an event db.ObjectAppended works is that whenever the database is appended with a new object, it will call the delegate function, which can be seen as a “function variable”, similar to normal object variables.
Delegate comes with the namespace, so whenever we use “using Autodesk.AutoCAD.DatabaseServices”, public delegate void ObjectEventHandler(object sender, ObjectEventArgs e); is included. Similar to “using System;” will include the common EventHandler delegate. Therefore, on AutoCAD, just use db.ObjectAppended += new ObjectEventHandler(DBOnObjectAppended);
4. Just a shorter form of 3, hide new ObjectEventHandler delegate.
So event and delegate are the same to trigger an event, but event makes more human sense to trigger an action when something happens. The difference is that event adds a layer of abstraction and protection on the delegate instance.
-Khoa
www.netscriptcad.com
Can't find what you're looking for? Ask the community or share your knowledge.