Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to position a form before it shows?

Tags:

forms

delphi

Our application used to make use of a common base form that all forms were meant to inherit from. I'd like to get rid of it for a number of reasons, ranging from the need to police that everyone uses it to several annoyances relating to Delphi's VFI implementation. It turns out that the bulk of the features it offered can be done in other, more reliable ways.

The one that I am not so sure about, is automatically positioning all forms in the center of their callers. So if I open Dialog A from my main form, it should be placed over the center of the main form. And if I then open Dialog B from Dialog A, it should be placed over the center of Dialog A and so on.

We used to take care of all this by setting the base form's Position property to poOwnerFormCenter and it worked great. But how do I do this app-wide?

I thought of using Screen.OnActiveFormChange, but I think this happens each time the form receives focus. I also thought of using Application.OnModalBegin but there doesn't seem to be an obvious way to find the form at the point this is called.

Has anyone tried this?

like image 443
Cobus Kruger Avatar asked Nov 03 '09 13:11

Cobus Kruger


People also ask

How do I change the position of the form during design time?

By dragging either the right edge, bottom edge, or the corner, you can resize the form. The second way you can resize the form while the designer is open, is through the properties pane. Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it.

Is used to set the position of form at run time view?

Remarks. This property enables you to set the starting position of the form when it is displayed at run time. The form's position can be specified manually by setting the Location property or use the default location specified by Windows.

How do you make a form always on top?

You can bring a Form on top of application by simply setting the Form. topmost form property to true will force the form to the top layer of the screen, while leaving the user able to enter data in the forms below. Topmost forms are always displayed at the highest point in the z-order of the windows on the desktop.

Which of the following option determines the position of form on screen?

When the application is started, the initial position of the form is determined by “StartPosition” property. Therefore, the “StartPosition” property is used to determine the initial position of the screen when it is started. Hence, the correct option is d.


2 Answers

Well, obviously form inheritance is provided to solve exactly the problem you're trying to solve. Any solution is probably going to wind up mimicking form inheritance in some way.

Could you do something as simple as globally searching your code for "= class(TForm)" and replacing the TForm class with either your existing base form or a new, simplified base form class with only the functionality you need?

Failing that, you could try to modify the original TForm class itself to have the positioning behavior you want. Obviously, modifying the supplied classes is a little on the dangerous side.

like image 159
Larry Lustig Avatar answered Oct 03 '22 16:10

Larry Lustig


If you are not going to go with a common base form, then I would suggest placing a non-visual component on each form. That component can inject the behaviors you want into the base form. If you want to have various different behaviors on different forms then give your component a role property that defines what role that form should have, and it can then inject different characteristics based on that role.

BTW, you can also have non-visual form inheritance, which is my preferred method of creating a common base class for all forms. It also has the advantage of adding properties to the form, and then based on those properties you can change the role or behavior of the form.

like image 30
Jim McKeeth Avatar answered Oct 03 '22 16:10

Jim McKeeth