Saturday, January 02, 2010

Neat tricks in the 'management' program/2 - MDI

I wrote a week ago about the use of non-modal dialogs, and how I was trapping the OnDeactivate event in order to minimise them. Whilst showing the program yesterday to the client, I discovered an unwanted side effect of this: it was impossible to show two opened child dialogs simultaneously. As one would open, the other would minimise; if I clicked on the second, the first would minimise. Ooops.

In a strange case of serendipity, the evening before found me playing around with MDI forms. I have never used this multiple document interface technique before, but it seems to match perfectly with the non-modal dialogs. Once I had realised my mistake yesterday, I realised that the entire program had to be revised in order to use the MDI model. The original main form did not use a menu, which precluded MDI, so I created a new main form, defined a menu and based all the MDI child dialogs on that menu. To make things easier for the client, the program automatically displays what was the main form upon starting. Of course, I removed from all the child forms the 'minimise on deactivation' event.

The code to activate these forms is becoming simpler and simpler. Contrast the code for a modal dialog box
with TModalDialog.create (nil) do
 try
  showmodal
 finally
  free
 end;

with the code for a non-modal dialog box
with TNonModalDialog.create (nil) do show;

and finally the non-modal MDI child dialog box
TMDIChild.create (nil);

After creating the MDI child, I call the main program's cascade method in order to ensure that the dialogs don't lie on top of each other. Each child dialog frees itself in its OnClose event.

Most articles about MDI assume that the MDI child dialogs will be identical - the easiest example of this for a Delphi programmer is the IDE itself; the windows displaying the code of the different modules are identical MDI child dialogs. But it doesn't have to be like that, and my program shows how to use non-identical child dialogs. Had those articles been clearer on the subject years ago, then I probably would have been a veteran MDI user and wouldn't have had to write this.

No comments: