Hi Guys,
I would like to do something when my paletteset is closed. I took the C# code from here
I modified the code to VB.net
Dim WithEvents ps As Autodesk.AutoCAD.Windows.PaletteSet <Autodesk.AutoCAD.Runtime.CommandMethod("psTest")> _ Public Sub PSTest() If ps Is Nothing Then ps = New Autodesk.AutoCAD.Windows.PaletteSet("psTest") ps.Dock = Autodesk.AutoCAD.Windows.DockSides.Left ps.Visible = True End If End Sub Private Sub ps_StateChanged(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.Windows.PaletteSetStateEventArgs) Handles ps.StateChanged If e.NewState = Autodesk.AutoCAD.Windows.StateEventIndex.Hide Then Application.DocumentManager.MdiActiveDocument.SendStringToExecute("CHECKPALETTESETCLOSE", True, True, False) End If End Sub <Autodesk.AutoCAD.Runtime.CommandMethod("CHECKPALETTESETCLOSE", CommandFlags.NoHistory)> _ Public Sub CheckPaletteSetState() If Not ps Is Nothing And ps.Visible = False Then Application.DocumentManager.MdiActiveDocument.Editor.WriteMessage("closed") End If End Sub
My code crashed. If I catch exception it is eInvalidInput exception. I don't know what to do as it is a simple code segment I don't know hot to fix it.
Any ideas?
Thanks
Hi,
first guess: don't check only for "Is Nothing" but also for "ps.IsDisposed"
HTH, - alfred -
Thanks for reply
the problem comes from here
Private Sub ps_StateChanged(ByVal sender As Object, ByVal e As Autodesk.AutoCAD.Windows.PaletteSetStateEventArgs) Handles ps.StateChanged If e.NewState = Autodesk.AutoCAD.Windows.StateEventIndex.Hide Then Application.DocumentManager.MdiActiveDocument.SendStringToExecute("CHECKPALETTESETCLOSE", True, True, False) '--here is the exception End If End Sub
Hi,
are you sure that you can run SendStringToExecute during this eventhandling? I fear NO ... to verify that: what happens if you send a _LINE-command?
PLUS: as it's a command defined by your app, why do you run this code with SendStringToExecute?
- alfred -
Thanks for reply. I don't know I took code from the post I found online(you can see in my main post). What I have done modify from c# to vb.net
I got reply from AutoCAD API support. They can replicate my issue. What I really want to do is to catch when user click paletteset "x" button rather than "-".
I don't know what to do
Hi,
in most cases when using events where I need to run SendCommand (or similar) I usually redirect the event to a position when AutoCAD get's quiescent.
So in your eventhandler create a new one using AddHandler that points to a sub handling then the reached quiesent-state for AutoCAD.
HTH, - alfred -
From the next TTIF post after the one in the link above.
Something I should have mentioned about the reason for implementing a command, in the first place: the standard StateChanged event gets fired twice when undocking a palette set – once to hide it and once to display it again. This means we can’t assume a hide notification means the palette set is being closed – we really need to wait until the dust has settled before checking the state of the palette set in order to proclaim our verdict. Which is why we’re sending a command to be executed
Thanks for your reply.
Have you try to implement that code segement? Does that work as it should be?
Sorry, I don't really like palettes, and they don't really fit in with the kind of automations I am writing. So, no, I have not tried the code to verify it's operation. I just replied to try and answer Alfred's question about why the code uses a SendStringToExecute.
The solution Kean posted on his blog has a few problems.
One is that he assumes that there is an active document to send a command to, which may not be the case since the PaletteSet could be visible in 'zero-document state' (e.g., there are no documents open). In that case, kaboom.
Another problem, which affects most kludges that involve sending strings to the command line, is that the command that closes the paletteset could be scripted by LISP, and what follows that could be a prompt from LISP requesting some input. Unfortunately, Kean's solution will send its command string to the command line while the LISP script is waiting for the user to type a response, and the command string ends up satisfying the input request.
You can avoid all of the nonsense related to that solution, by trying this one (and remember that you can't assume there is any open documents when a PaletteSet is closed):
Getting notified when a PaletteSet is hidden/closed