Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to have more than one modal dialog?

in one of our web applications we have a base frame we use for showing modal dialogs. The base frame contains a TIWModalWindow component with IWFrameRegion as its ContentElement, as well as common code for logging and language support. In descendant frames we add visual elements, decide which buttons to use (e.g. OK & Cancel), add code to the ModalWindow.OnAsyncClick event handler etc. A using form creates such a frame (at runtime) when it wants to show it and calls a method ShowModal() of the frame (which sets Visible = True and calls ModalWindow.Show()). This has worked nicely.

Now we have a form where we want to be able to show more than one modal dialog (although not at the same time). You should be able to change data in the GUI of dialog 1, close it, open dialog 2, do some changes, close it and reopen dialog 1 with your earlier changes retained, etc. However, the rendering fails as soon as I try to open the second dialog. I have attached a simplified example project that demonstrates this. I have tried various things in the OnAsyncClose event handler such as setting Parent to nil for the frame, setting ModalDialog.ContentElement to nil, calling ModalWindow.Reset() etc. but with no luck.

Is there a way of having more than one modal dialog "in memory" like we want here? Has it something to do with multiple instances of the TIWModalWindow component? Please advice on what we need to change in order to solve this problem.

Best regards

Magnus Oskarsson

Attached Files
.zip (Size: 5.75 KB / Downloads: 2)
Of course in order to show a modal dialog it needs to be be on the form (hidden) prior to being used.  If you want to show more than one modal dialog then set up more than one on the form to begin with.

Then invoke a dialog as you would if it were the only one.  The other stays hidden just as would any other hidden components.

I load my "common" dialogs by calling a procedure in a form load event.

I took a quick look at your code.  Please see the IW demo "ModalWindow" on github.

The code to show a region looks like this:

with IWModalWindow1 do begin
    Buttons.CommaText := '&OK,&Cancel';
    Title := ATitle;
    ContentElement := ARegion;
    OnAsyncClick := WindowDoOnAsyncClick;
    FriendlyName := 'mydlg';
    CloseButtonVisible := False;
    CloseOnEscKey := False;

I see you are using "showmodal".  You should do a reset, then load the elements and show as in the demo.

Hi Dan and thanks for your suggestions! Your suggestions do not work right away if I apply it to the ShowModal method in my base frame, I still get the same rendering error. But I found the example you referred to and saw that they use the same TIWModalWindow instance for different purposes. So I tried to move the component from the frame to the form (so that there is a single instance) and let the base frame have a ModalWindow property instead. This was not enough however, I also had to make sure that I removed the previously showed frame from its parent control before showing the other. I have not had time to investigate why this is so, although I suspect it could have something to do that we have more than one control named IWFrameRegion (inside the frames) that we use as ContentElement. I attach a new version of my test program which functionally seems to be working OK now. I will check if I can apply this kind of change to my real world application as well.


Attached Files
.zip (Size: 9.91 KB / Downloads: 3)
It is correct that you cannot have more than one component with the same name on a form. This is an HTML fact of life. That goes for any control. Names must be unique on the page.

Also correct that you only use one TIWModalWindow component on the form, as in the demo. If you set the frame (and it's top region) and use .Show as in the example then you should find that it works well.


Forum Jump:

Users browsing this thread: 1 Guest(s)