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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With