Your code is working perfectly but my RemoveHandler part is not working... In my form I have a "OFF" button which fire's off the "RemoveHandler" part. Even after triggering the "RemoveHandler" the program is still issuing the "CommandWillStart" event. Here's my (fairly simple) problematic code I had written so far. I've chopped down most of the code that does not relate to the eventhandler. Is there any inconsistency concerning the "RemoveHandler" function?
You're help is much appreciated... Thanks!
Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.ApplicationServices.Core.Application
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Geometry
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.ApplicationServices.Application
Imports System.Windows.Forms
Imports Autodesk.AutoCAD.Colors
Imports System.IO
Imports Autodesk.AutoCAD.Runtime
Public Class Form1
Implements IExtensionApplication
Private CurrentLayerName As String
Private _SelectedLayer As LayerData
Private Shared acDoc As Document
Public Sub Initialize() Implements IExtensionApplication.Initialize
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("Initialize")
acDoc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument
End Sub
Public Sub Terminate() Implements IExtensionApplication.Terminate
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
_SelectedLayer = New LayerData
End Sub
Private Sub myOnButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles myOnButton.Click
AddHandler acDoc.CommandWillStart, AddressOf SwapTheLayer
End Sub
Private Sub OFF_Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OFF_Button.Click
RemoveHandler acDoc.CommandWillStart, AddressOf SwapTheLayer
End Sub
Private Sub SwapTheLayer(ByVal senderObj As Object, ByVal e As CommandEventArgs)
CurrentLayerName = Autodesk.AutoCAD.ApplicationServices.Application.GetSystemVariable("CLAYER")
Dim importedLayer As Array = _SelectedLayer.ToArray
Select Case e.GlobalCommandName
Case "MTEXT"
If checkIfLayerExistInDrawing(MText_TextBox.Text) = True Then
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("layer exist")
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", MText_TextBox.Text.ToString)
Else
Dim myCreateLay As New CreateLayer(importedLayer(0).MyLayerName,
importedLayer(0).MyLayerDescription,
importedLayer(0).MyLayerColor,
importedLayer(0).MyLayerPlot,
importedLayer(0).MyLayerLinetypeName,
importedLayer(0).MyLayerLineweight)
Autodesk.AutoCAD.ApplicationServices.Core.Application.SetSystemVariable("CLAYER", MText_TextBox.Text)
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("layer do not exist in drawing")
End If
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("AddHandler CommandEnded")
AddHandler acDoc.CommandEnded, AddressOf BackToOriginalLayer
AddHandler acDoc.CommandCancelled, AddressOf BackToOriginalLayer
AddHandler acDoc.CommandFailed, AddressOf BackToOriginalLayer
End Select
End Sub
Private Sub BackToOriginalLayer(ByVal senderObj As Object, ByVal e As CommandEventArgs)
Select Case e.GlobalCommandName
Case Is = "MTEXT"
Autodesk.AutoCAD.ApplicationServices.Application.SetSystemVariable("CLAYER", CurrentLayerName)
Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog("RemoveHandler CommandEnded")
RemoveHandler acDoc.CommandEnded, AddressOf BackToOriginalLayer
RemoveHandler acDoc.CommandCancelled, AddressOf BackToOriginalLayer
RemoveHandler acDoc.CommandFailed, AddressOf BackToOriginalLayer
End Select
End Sub
Private Function checkIfLayerExistInDrawing(ByVal myLayerName As String)
Dim LayerExist As Boolean
'' Get the current document and database
Dim acCurDb As Database = acDoc.Database
'' Start a transaction
Using acTrans As Transaction = acCurDb.TransactionManager.StartTransaction()
'' Open the Layer table for read
Dim acLyrTbl As LayerTable
acLyrTbl = acTrans.GetObject(acCurDb.LayerTableId, OpenMode.ForRead)
'layer exist in drawing...
If acLyrTbl.Has(myLayerName) = False Then
LayerExist = False
Else
LayerExist = True
End If
End Using
Return LayerExist
End Function
End Class