[求助] WithEvents 事件
<p>我在autocad vba 模块中编写如下代码</p><p>Public WithEvents PLine As AcadLWPolyline ' Use with Modified Event Example<br/>Sub Example_Modified()<br/> ' This example creates a light weight polyline in model space and<br/> ' references the new PolyLine using the public variable (PLine) which<br/> ' is setup to intercept Modified events.<br/> '<br/> ' This example then modifies the new object, triggering the code<br/> ' in the Modified event.<br/> <br/> Dim points(0 To 9) As Double<br/> <br/> ' Define the 2D polyline points<br/> points(0) = 1: points(1) = 1<br/> points(2) = 1: points(3) = 2<br/> points(4) = 2: points(5) = 2<br/> points(6) = 3: points(7) = 2<br/> points(8) = 4: points(9) = 4<br/> <br/> ' Create a light weight Polyline object in model space<br/> '<br/> ' * Note: We are returning the new PolyLine object into a Module<br/> ' level variable. This allows us to intercept events associated<br/> ' with that particular object.<br/> Set PLine = ThisDrawing.ModelSpace.AddLightWeightPolyline(points)<br/> <br/> ThisDrawing.Application.ZoomAll<br/> <br/> ' Modify object to trigger event.<br/> '<br/> ' * Note: The event code for the PolyLine modification will be triggered<br/> ' before we move forward and refresh the view, so the line will not<br/> ' appear red when the event message box is displayed<br/> PLine.Color = acRed</p><p> ThisDrawing.Regen acAllViewports<br/> <br/>End Sub</p><p>Private Sub PLine_Modified(ByVal pObject As AutoCAD.IAcadObject)<br/> ' This example intercepts an object's Modified event.<br/> '<br/> ' This event is triggered when an object supporting this event is modified<br/> '<br/> ' To trigger this code: Modifiy an object connected to this event<br/> ' * Note: By connected, we mean the object setup to intercept events using<br/> ' the VBA WithEvents statement</p><p> ' Use the "pObject" variable to determine which object was modified<br/> MsgBox "You just modified an object with an ID of: " & pObject.ObjectID<br/> <br/>End Sub</p><p><br/>调试时提示 错误,怎末回事呀?</p><p>谢谢赐教!</p> <p>本人已经解决了:</p><p>1、在类中增加一个“eventclassmoduel”类,</p><p>2、把Public WithEvents PLine As AcadLWPolyline ' Use with Modified Event Example</p><p>放入“eventclassmoduel”中</p><p>3、在标准模块中定义引用即可:dim x as eventclassmoduel</p><p>4、sub xxx()</p><p>................</p><p>’根据需要添加代码</p><p>end sub</p><p><br/></p> 把整个代码放在thisdarwing里不就可以了,不用搞那么麻烦!
页:
[1]