dcl-Tree-GetSelectedItem on Cancel and/or Close

Started by Peter2, November 19, 2014, 01:55:54 PM

Previous topic - Next topic

Peter2

I use a tree and want to save the SelectedItem when I close the dialog with the "Windows close X" in the right upper corner of the dialogue.

I tried this code:
Code (autolisp) Select

; I tried also with defun c:sp_haupt#OnClose
(defun c:sp_haupt#OnCancel ()
    (setq baumstatus (dcl-Tree-GetSelectedItem sp_Haupt_tree))
)

(defun c:sp_haupt#OnInitialize ()
    (if baumstatus
        (dcl-Tree-selectitem sp_Haupt_tree baumstatus)
    )

Result: nothing - "baumstatus" stays on nil ..

Where do I have to search my error-of-the-day?

Many thanks
Peter
ODCL 8.1.... German, AutoCAD 2017 - 2018, Win 7 x64, Win 10

owenwengerd

I would start by verifying that the (dcl-Tree-GetSelectedItem sp_Haupt_tree) gets called. Once that is verified, I would check that the return value is indeed NIL, (to ensure that baumstatus is not being changed afterward). If all checks out, I would then try the same diagnostics from a button's OnClick event handler to see if the problem is due to timing or some other reason. If it's due to timing, I recommend to upload a simple sample and detailed instructions that I can use to reproduce it.

Peter2

Hi Owen

I made some researches - it seems that the command "dcl_form_close ..." causes the problems.

If I use the command "(dcl_form_close mydialog)" in the commandline, then I get an error messsage:
Code (autolisp) Select
Command: (dcl_Form_Close spdrs_Haupt)
; Error: Too many arguments
(303.0 206.0)

If the command is included in the lisp-code, then I get an error in the lisp. If the command is included in an "event", then I have a crash of AutoCAD with "Access violation".

I made a screencast to demonstrate the problem:
https://screencast.autodesk.com/Main/Details/fd884eb9-571c-477f-84e7-28b7e6c5c71a

The test-code of LSP and ODCL are attached.

Anyone else who has problem with "dcl_form_close"?
Peter
ODCL 8.1.... German, AutoCAD 2017 - 2018, Win 7 x64, Win 10

owenwengerd

I have not yet tested your code, but I suspect the "too many arguments" error comes from an incorrect event handler that is fired during (dcl-Form-Close). Probably one of your event handlers is getting arguments that it does not expect.

owenwengerd

#4
Some comments after looking at your code:

1) Your OnClose event handler is incorrect. It will receive two arguments, but your code expects no arguments. This is undoubtedly the reason for the "too many arguments" error.
2) It's a good idea to not use the (alert) function for diagnostic purposes, as it can have inadvertent effects on the thing being diagnosed.
3) When adding diagnostic code, be careful to not change the return value of events where the return value matters (like OnCancelClose).
4) There is no need to call (dcl-Form-Close) in the OnClose, OnCancel, and OnCancelClose event handlers. In fact, it could lead to an infinite loop in OnCancelClose if the handler returns NIL to prevent closing.

Hopefully this will get you back on track with the original issue.

roy_043

2 additional remarks:
5) Your using both the old and the new naming convention. You have 'c:Haupt#OnCancel' but also 'c:Haupt_OnInitialize'. This can only lead to confusion.
6) You are not localizing your variables.

Peter2

Quote from: owenwengerd on November 20, 2014, 06:37:50 AM
Some comments after looking at your code:

1) Your OnClose event handler is incorrect.....
That's it - there was a really mess in the code. I cleaned it up - and learned about the sequence of OnCancel, OnClose, form_Close. Now the first and the second problem is solved.

Probably a "greenly" question, but is there a kind of flow-diagram which displays the base of "load / show / hide / show / close / cancel / close" ...?

Quote from: roy_043 on November 20, 2014, 06:59:49 AM
2 additional remarks:...
Yes, I started with version 8 yesterday, using elder code snippets. But the confusion is caused by me and not be the naming convention. I have to clean it up and to localize my variables too.

Thanks for your help, Owen and roy_043.
Peter
ODCL 8.1.... German, AutoCAD 2017 - 2018, Win 7 x64, Win 10

owenwengerd

There is no flow diagram, but the typical usage pattern for modal and modeless forms is discussed in the documentation and demonstrated in the samples.

Fred Tomke

#8
Hi,
Quote from: Peter2 on November 20, 2014, 08:00:17 AM
[...]
Probably a "greenly" question, but is there a kind of flow-diagram which displays the base of "load / show / hide / show / close / cancel / close" ...?
[...]

not sure, if you meant something like this, but may be this makes it more clear:




















OnInitializeIt is called after form dcl-form-show method was called and all controls are available.
Please note: Between dcl-form-show and OnInitialize already some events are fired (TabSelChanged for instance).
Please note: It is not called after (dcl-form-hide oForm nil).
OnCancelCloseIt is called after form dcl-form-close method was called or Enter or ESC are pressed in a control which doesn't handle these keys. Then this keypress will be lead to the form and handled there. In OnCancelClose you have the chance to "think about do I really want it" closing the form. For instance, if some information are missing you can show a messagebox or whatever. If you press ESC in a grid in editable state you can cancel grid editing and leave the form open. To suppress form closing you have to return true. To avoid closing the form after hitting Enter in a textbox or grid, I check the first argument intIsEsc and return (/= intIsEsc 1).
OnOkIt is called after OnCancelClose and before OnCancel and OnClose.
OnCancelIt is called after OnCancelClose, after OnOk (after hitting Enter in Textbox) and right before OnClose. Unfortunalety it seems to be called twice after pressing ESC, but only once when clicking the cross in upper right corner or clicking the or clicking the Close form button. And unfortunately, it is also called after hitting Enter in textbox (just between OnOk and OnClose).
OnCloseIt is called when the form is finally going to be closed - just right before none of the controls are available. It is just the last second to get the current values from the controls before they're getting destroyed.
ShowMethod to create a new form instance. Note that you're responsible to check with dcl-form-isactive, if there's already an existing instance.
CloseMethod to close and destroy an existing form instance.
HideChanges the visibility of a none-modal form. It is not available for modal forms, because the control cannot send back to AutoCAD.

Have a short look at the sample FormEventTester I've attached. And please note the return values of dcl-form-show!
Hope that helps a very little bit.

With regards,
Fred
Fred Tomke
Dipl.-Ing. (FH) Landespflege

[ landscaper - landscape developer - digital landscape and urban design]

khann

Hi,
I'm just watching this post.
But found something odds.
Some code of Peter2's written " (dcl_Form_Close ... ")
and Fred's sample codes shown as

(dcl-form-close FormEventTester/MyForm 2)

And My ACAD does not read Fred's sample.

May wish someone tell me what is this difference of "-" and "_" ?

(On using OpenDCL.Studio.ENU.7.0.1.4)

Thanks.

owenwengerd

Quote from: khann on November 22, 2014, 07:29:30 PM
May wish someone tell me what is this difference of "-" and "_" ?

This is new in OpenDCL 8.0. See here.