Is it possible to edit a DFM (Delphi's form script format) in such a way that a form closes itself when shown?
I don't code in Delphi, so I'm not familiar with how these forms work, but it seems I could put code (but not standard Delphi code as it seems) in the OnShow or OnCreate events of the form. However, after trying several statements like Close, Exit, FormNameExit, Destroy, etc. won't work (a log will be created, stating the error that the value of the OnShow property was invalid, etc.)
The normal way of closing the form is through a button, but the button doesn't have a OnClick event, just a property, "ModalResult = 1".
Is there a way to make the window close upon opening, some standard function I could put on the OnCreate or OnShow events of the form? Or maybe, creating a checkbox on the form itself, that gives ModalResult = 1? (don't know if this works)
Thanks for any suggestion!
=)
(Note: maybe it's obvious, but I don't have the source.)
EDIT: Seeing some of your comments added while I typed my text-wall clarifies things a bit.
I'm guessing that you you're using a resource editor to edit the DFM and modify the behaviour of the app without actually touching the source code?
In this case, the best you could try is to set the
Visible
property toFalse
. However, this will have no benefit if the developer 'actively displays the form in code'. (He could have done this by callingShow
,ShowModal
or even by explicitly setting theVisible
property.)Unfortunately, if this is the case, then there's nothing you can do without modifying the actual source code. This is because the DFM is processed when the form is loaded; i.e. before the developer's code that shows the form. Even looking for a place to set
ModalResult
is useless, because the currentModalResult
is cleared whenShowModal
is called.
I don't think I understand exactly what you're trying to do, because it doesn't make sense.
It seems to me that you want the form to be automatically closed as soon as it is shown; and that doesn't make sense. :S
So, if I have understood you correctly, please explain why you would want to do this; there may be a better solution for your actual problem.
However, some general concepts...
ModalResult
.ModalResult
has no effect.FormStyle
being the most important) that have an effect on how it behaves, displays, and can be closed. (E.g. MDI Child forms will by default either minimise, or do nothing when closed.)Now, some of the technicalities...
MyForm.Show;
or ModalResult := MyForm.ShowModal;
ModalResult
to find out the user's answer and act accordingly.ModalResult
and the form will close itself. An easy way to do this is to assign a ModalResult
to the buttons on the form; then the button will automatically set the form's ModalResult
when clicked.MyForm.Close
at the appropriate point in time. NB: Note their are other ways to 'close' a form, but it is better to use this method because it allows you to handle the OnCloseQuery
event which is a mechanism to confirm whether the form is allowed to close.OnCloseQuery
is called to confirm whether the form is allowed to close.OnClose
is called to find out how the form should close (as explained previously).Coming back to your question (which sounds like you want the form closed automatically). Rather than closing the form automatically; just don't bother showing it. This is very easy to do. All forms have a Visible
property; if set to True
, Delphi will automatically show the form normally when it is created. So all you need to do is ensure the property is False
.