Delphi Mdichild flashing form when opening in modal mode

Asked

Viewed 421 times

1

I have a form MDIChild which I open as follows:

Application.CreateForm(TfrmManProduto, frmManProduto);

However, sometimes I need this form in modal... with some tips I adapted my code the following way to open it in modal mode:

Application.CreateForm(TfrmManProduto, frmManProduto);  
frmManProduto.FormStyle   := fsNormal;                 
frmManProduto.Visible     := False;
frmManProduto.Position    := poMainFormCenter;
frmManProduto.ShowModal;

That way it works, but it blinks until it shows on the screen.

I wonder if there is any way to not flash/show the form until you reach the call frmManProduto.ShowModal;

1 answer

2


Set the property Visible and FormStyle of the form as False and fsNormal respectively by default and create a constructor method that takes a parameter stating whether the form will be modal or not, something like that:

type
  TfrmManProduto = class(TForm)
  public
    constructor Create(AOwner: TComponent; isModal: Boolean); reintroduce;
  end;

implemetation

TfrmManProduto.Create(AOwner: TComponent; isModal: Boolean);
begin
  inherited Create(AOwner);

  Position := poMainFormCenter;

  if not (isModal) then
  begin
    FormStyle := fsMDIChild;
    Visible := True;
  end;
end;

To create the form would be like this:

frmManProduto := TfrmManProduto.Create(Application, True);
frmManProduto.ShowModal();
  • good morning Roberto, I did as you passed, however, on the lines frmManProduto.FormStyle := fsNormal; and Visible := True; generates Exception... tried to add inherited Create(AOwner); inside the constructor but it didn’t solve.. I think I’m missing something..

  • the frmManProduto is still NIL in such situations, so does not accept amendments..

  • 1

    I changed it, now it should work, I put together the code kind of running and I didn’t realize I was referencing the variable, I had also forgotten the inherited Create. But this corrected in the reply

  • 1

    worked, but, in modal form he creates, shows on the screen but does not get modal... I tried to put a showmodal after creation but presents error, I tried to put after set position, in this case it opens modal, but when closing it calls the form ONSHOW AND ONCREATE... I found it strange and could not figure out the reason...

  • The ShowModal it has to be after the Create and not during, I changed the answer, I put the ShowModal for example.

  • I tested this way, and I get the message 'Cannot make a Visible modal window', in the call frmManProduto := TfrmManProduto.Create(Application, True); it already creates the form and shows on the screen if I add frmManProduto.ShowModal(); he returns me the message above...

  • @Felipesachetti changed the answer, now I was able to test on my machine and it worked.

  • perfect... mto thanks Roberto, solve my problem...

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.