Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception while positioning form at desktop center

My main form is launched with WindowState as wsMaximized and Position as poDefault. When I click on maximize/minimize toggle button, I want my diminished form to be placed at desktop center. So in OnResize I placed the following:

if WindowState = wsMaximized then
    Position := poDefault
 else if WindowState = wsNormal then
    Position := poScreenCenter;

When my program starts, I get this exception: 'Cannot change visible in OnShow or OnHide'.

What should I do to make my form either maximized or normal and centered?

like image 412
Max Smith Avatar asked Dec 30 '25 10:12

Max Smith


1 Answers

FormResize method occur when your form displaying at first time. You must check current state of form and do not try to resize it when creating or showing at first time

procedure TForm1.FormResize(Sender: TObject);
begin
    if not(fsVisible in Self.FormState) then
      Exit;

    if WindowState = wsMaximized then
      Position := poDefault
    else
      if WindowState = wsNormal then
        Position := poScreenCenter;
end;

Read more at http://docwiki.embarcadero.com/Libraries/en/Vcl.Forms.TFormState

The following table lists the values that can be included in a form's state:

Value Meaning

fsCreating -- The form's constructor is currently executing.

fsVisible -- The form's window is visible. This state is used to update the Visible property.

fsShowing -- The form's WindowState property is changing. This state is used to prevent WindowState changes from interfering with a transition that is in progress.

fsModal -- The form was created as a modal window.

fsActivated -- The form has received focus or the application became active but has not yet called the Activate method to generate an OnActivate event.

like image 67
Zam Avatar answered Jan 02 '26 04:01

Zam