Advanced Xailer Techniques, What happen during form creation ?
[Original post by José F. Gimenez]
Many of you are using OnInitialize event in the forms to do some control initialization, asigment of initial values, etc... But some of you are using it because someone has told you to do so, and don't realize really why you should do it.
In this article I'll try to clarify all the steps that happen during the process of forms creation, including the moment in which some events are launched. In this way all of you will notice where and when to use one event or other.
Back to the basics.... New(), Create(), Destroy() and End(). These four methods are the ones implied in Xailer's controls creation and destruction.
- New() is the method which instances and initialize the control container. You just have to run the class function to instance the object, but the call to New() method, even is a tradition in xBase, also is used to indicate the "parent" of the control, and perform certain initialization of the control.
- Create() This is the method that really creates the control. Between New() and Create() all the control's needed properties are asigned, so, at the very creation moment, all the properties are already asigned with a value given by ourselves, or with the default value.
- Destroy() As you can imagine, this method destroys the control, but it doesn't free the resources used, nor destoys the container object.
- End() is he one which frees all the resources used by the control (memory, images, etc.) On the other hand, remember that under xbase, you cannot destoy the object directly from himself, but notice all the time that once End() executed, the object cannot be used again.
So far so good, these applies to controls, but in the case of non-visual components, New() and Create() can be used all alike, due to the most of these components don't have an equivalent with any Windows object or control.
Talking about forms, there's also an important difference: You NEVER call Create() directly, you just need to call New(), because CreateForm() method is the one which calls Create(), we will be back on this later.
Ok, once we had clarified which methods are implied in forms and controls creation, let's see, step by step what happen when you execute a line like:
TForm1():New( Application ):Show()
New() intances and initializes the object. In the case of forms, it internally calls CreateForm() method, this method is written in the .XFM file. This .XFM file is just a source code file with the same name of the .PRG file, .XFM stands for "Xailer ForM". The main reason cause this file is separated from the .PRG is because it can be handle by the IDE in a easier way, after all, we don't have to modify it by ourselves, we just have to know what is it, and what does it have, and if you are curious enough and edit it, you will see something like this:
METHOD CreateForm() CLASS TForm1
Super:CreateForm()
::SetBounds( 220, 120, 450, 300 )
::cText := "Form1"
::oFont := TFont():Create( "MS Sans Serif", 8, 0, 400 )
::nClientWidth := 442
::nClientHeight := 266
::Create()
::oFileOpenDlg1 := TFileOpenDlg():Create( Self )
WITH OBJECT ::oMemo1 := TMemo():New( Self )
:SetBounds( 0, 0, 442, 266 )
:nAlign := alCLIENT
:Value := ""
:Create()
END
RETURN Self
As mentioned before, the XFM file just contains the CreateForm() method. Please notice that it also calls Super:CreateForms() and ::SetBounds(), and right after these calls, some properties of the form are asigned and then ::Create() is called.
But here is where the first event comes: OnCreate. Just in the call to Create() method this event is launched, as you can notice, in spite the form is created, none of its components or controls are created yet, so if you need to do something in this point, you can use OnCreate event, but be aware that at this point you cannot modify any component, simply because they still don't exist, and you will get an error.
Right after, the creation of the TFileOpenDlg component comes. As stated before, you don't need to call New() and Create(), because it is instanced and created when you call Create().
Please notice that non-visual components should be created BEFORE to create the controls, the reason is simply, some of these components are used by the controls, for example a TDbfDataSource or a TDbfDataSet.
And finally we find the creation of the TMemo() control. In this case we need to instance the object calling the New() method, asign its properties, and finish it calling to Create(), to create the control, at this moment, the event OnCreate is launched, so you can use it if needed.
Once finished with the CreateForm(), the form al all its controls are created, and here is when OnInitialize event is launched, so when you need to perform an initialization task over an already created control, you can use this event. At this point, the form still is not visible.
After that, the events that are launched are: OnShow, when the form is visible by calling the methods Show() or ShowModal(), and OnActivate, when the form got the keyboard focus. These two events are not only launched when the form is created, they will be launched whenever needed.
Well, with this lesson you have already learned all the steps until the form is visible and got the focus. If you have learned this, you will now have learned a very important part of how Xailer works.
0 Comments:
Post a Comment
<< Home