S.FATEH
S.FATEH

Reputation: 451

Error in call to overridden ShowModal while showing modal

I'm working with a modal form that is shown as Full Screen. I manage to do that by overriding the virtual ShowModal() method.

function TfrmComptoir.ShowModal: Integer;
begin
  FullScreen := ReadFromIni('Config.ini', Self.Name, 'FullScreen', False);
  if FullScreen then
  begin
    BorderStyle := bsNone;
    WindowState := wsMaximized;
    width := Screen.Width;
    Height := Screen.Height;
  end else
  begin
    BorderStyle := bsSizeable;
    WindowState := wsMaximized;
  end;

  Result := inherited;
end;

This is the procedure where I show the form:

procedure TfrmPrincipal.btnComptoirClick(Sender: TObject);
begin
  frmComptoir := TfrmComptoir.Create(nil);
  try
    frmComptoir.ShowModal;
  finally
    FreeAndNil(frmComptoir);
  end;
end;

On my modal form, I have a button to let the user switch between Full Screen and Normal mode. Here is the problem. I cannot call the ShowModal() method again, as I get an error:

cannot make a visible window modal

What can I do to fix this?

Upvotes: 0

Views: 1054

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596352

You cannot call ShowModal() while the Form is already showing. Unlike Show(), ShowModal() can only be called once at a time, the Form must be closed before ShowModal() can be called.

What you can do instead is move the property-twiddling code into a method of its own, and then call that method inside of both ShowModal() and the button's OnClick handler, eg:

function TfrmComptoir.ShowModal: Integer;
begin
  SetFullScreen(ReadFromIni('Config.ini', Self.Name, 'FullScreen', False));
  Result := inherited;
  WriteToIni('Config.ini', Self.Name, 'FullScreen', FullScreen);
end;

procedure TfrmComptoir.Button1Click(Sender);
begin
  SetFullScreen(not FullScreen);
end;

procedure TfrmComptoir.SetFullScreen(Value: Boolean);
begin
  FullScreen := Value;
  if FullScreen then
  begin
    BorderStyle := bsNone;
    WindowState := wsMaximized;
    Width := Screen.Width;
    Height := Screen.Height;
  end
  else
  begin
    BorderStyle := bsSizeable;
    WindowState := wsMaximized;
  end;
end;

Upvotes: 3

Related Questions