In the OP's ERP management system: there can be several forms displayed at the same time and all have the same background colour, so if one is not paying attention, it can be difficult to know which is the active form.
In an application that has a main form and several son forms, all of which are modal, then this active form problem does not exist as only one modal form can be displayed at a given time (that's not 100% true: a modal form can cause another modal form to be displayed on top of the first, but this is unusual and possibly confusing). But in an application where there are several non-modal forms (they don't have to be MDIChild forms), a few can appear on the screen simultaneously, of which only one is the active form.
How to solve this? I thought at first of using
screen.activeform but this isn't a particularly good idea.
Whilst it might allow discovering which form is active, it won't restore the
previous active form when the active form loses focus (not necessarily
closed).
It turns out that there is a pair of methods than solve this problem - OnActivate and OnDeactivate. I have come across OnActivate in the context of modal forms although for them, this method doesn't add anything to OnCreate or OnShow. But with non-modal forms, this is a good mechanism. As CoPilot tells me,
OnActivate is raised not only when a form is first shown, but whenever the user switches to it:
- by clicking it
- via Window menu
- after another form closes
- after minimizing/restoring another child
Of course, OnDeactivate is invoked by a form when it is about
to lose focus. Using this pair of methods, the necessary code to change the
background colour of not-active forms becomes very simple.
Procedure TStickyForm.HandleActivate (Sender: TObject); begin inherited; color:= clMoneyGreen; end; Procedure TStickyForm.HandleDeActivate (Sender: TObject); begin inherited; color:= $008FBC8F; // clDarkSeaGreen ?? end;
TStickyForm is the parent form from which most of the non-modal forms inherit, so all of them automatically get this behaviour. There are one or two non-modal forms that don't inherit from TStickyForm for reasons that I don't recall, but it's easy to add these two methods to their code.
In a sense, this is similar to this code1 for modal forms, although that solution simply causes the screen's background to become grey whilst displaying the modal dialog form in front of the background.
Internal links
[1] 1827
| Title | Tags | ||
|---|---|---|---|
| 424 | The thousand-dollar penalty for reusing passwords | Computers | |
| 1087 | More thinking on the page (DBA) | DBA | |
| 1182 | Protecting cells in Excel files | Programming, Delphi, Excel, Office automation | |
| 1354 | Overcoming the 'leading zero' leads to other problems with Excel | Programming, Excel, Office automation | |
| 1547 | Have I found the solution to a cooking problem? | Cooking, Meta-blogging | |
| 1684 | YouTuber's first listens to Van der Graaf Generator | Van der Graaf Generator |