form main + form child stacking issue - delphi

I am using main form and child forms.
If I open 2nd child form with
TMYForm.Create(nil);
It does not stack it on the 1st child form top.
Is there any way for 2nd and X'th child form to appear in exact same position as 1st child form?
Or I need to destroy old child form while creating new one (as stacking is done automatically)?

That is the expected behavior. Without specifying otherwise, Forms will open on the right and down from the position of the previously opened.
If you want to control the position of your form, change its Position property to poDesigned, but you have to set its Top and Left properties to ensure it'll be visible.
Or you can used some presets: poDesktopCenter, poMainFormCenter, poOwnerFormCenter or poScreenCenter.

Try putting this:
TMYForm.Position:=poMainFormCenter;
or
TMYForm.Position:=poOwnerFormCenter;
Before TMYForm.Create(nil);
-S

If you have references to the child forms, use Form2.BoundsRect := Form1.BoundsRect

Related

Parent components show over child form

I've been following the below page in order to have a child Form that I can click on its components (such as an edit) and have proper focus:
Working with MDI Forms in Delphi
The problem now is that the components on the parent Form show on top of the child Form. If I use the SendToBack() command for those components, they completely disappear. Here is a screenshot of what happens:
EDIT:
I have a Form with buttons and a ListView that displays client info. When I click on a client, I can click a button to view or edit that client, alternatively add a new one. The Edit/Add pops up a Form where I can input the info and save it. I'm using an OnClick event for each Edit that has a SetFocus(). That gets focus on each Edit, but then all the text is selected, so I cannot click on the line and start editing text - it just overrides unless I use an arrow first.
Initially, I used regular Forms and that's where I had the focus issue. MDI fixed that, but now it's the parent components that show on top of the child.
Concerning MDI forms: that is how MDI works. The main form is not supposed to have anything else than a menu bar and optionally a toolbar, aligned alTop. The child forms use the rest of the space. Well as #RemyLebeau suggested, you can add other controls too, if you align them.
But it turned out that the actual problem with using ordinary secondary forms was that the text in an edit control on the secondary form becomes selected when you set focus to the edit control in a button click. That is easy to change, right after you set the focus to an edit control:
Edit2.SelLength := 0; // nothing selected, cursor at beginning
Edit3.SelStart := Length(Edit3.Text); // nothing selected, cursor at end of text

Delphi - move control to the ancestor form

I have more than 50 forms which have the same button on them. All of them are derived from the same ancestor. Is there any automatically way to move that button(or any other control) to the common ancestor?
David Miro had the right answer, but I think he misunderstood what you want to do. You are not trying to move the position of the butttons.
If you have not edited the buttons on the child form(s), you can add a new button on the parent. It will appear automatically on each child form. It will be a new button, which must have a different name but there will be an inherited button on each form. Then you will want to edit each child form to remove the original button. You will have a button and it will be inherited. If the event handler is always the same, you can code that into the parent as well.
If you have edited the buttons on the child forms previously, you can do this. The only way I know is to edit the DFM file of the form. A button declared in the form is defined without any reference to a parent. An inherited button is defined with an INHERIT in front of it. You need to add the INHERIT word, which tells the form that the button is inherited. If that sounds complicated, just create 2 buttons and look at the difference. It's not really too complicated.
The difficulty is this: you can't inherit from something before you create it (the parent button). And, you may have some difficulty creating the parent because the children already have a component with that name. You can change the name if you have to. But this can be done. I think this is what you are looking for. Strange there's no easier way to do this, because improvements like this are often created in the child form first.
At design time no problem. If you move parent button position , automatically moves the children buttons. But if you moved the child button, then this no longer works.
A solution. Although tedious, is to edit the form dfm child file and remove the attributes you need to inherit from dfm parent file (button.left, button.right, etc. ..)
With this procedure get it working again

Delphi - child forms have frame of parent

I have a delphi application with multiple forms. Initially I had tried a setup where each newly opened form was a frame and the "parent" of this form (whichever called to open the form) was hidden as the child was shown with the child being resized and relocated to give a seamless effect of having one window, when the child is closed the parent is relocated and again made visible. All forms have a bsSingle border style for the Windows title block.
This approach worked well for positioning however the issue I have is a noticeable flicker as the parent form is closed and the child opened, and as there is a small time period where no form is opened the icon/tray on the start bar would shift around and itself become hidden and visible.
Does anybody have any advice on solving this problem? I thought perhaps if I only had one form with the border within the application and opened each new form within this border it would work better - though I am unsure how exactly to do this.
Any help is much appreciated.
It is easy to make one form appear as a child inside another. Create a new form which will contain and create your other forms:
procedure TMainForm.FormCreate(Sender: TObject);
var
F : TForm;
begin
F := TOneOfYourChildForms.Create(Self);
F.Parent := Self;
F.Show();
end;
Create both your child forms similar to this, then just do Show on the one you want to display and Hide on the other. Set BorderStyle to bsNone on the child forms to remove the caption. Turn off Auto-Create on your forms in project settings if you create them yourself like this instead.
I've had success with this design, and I think it helped to have the contents of the "main form" within a TFrame as well. When you want to show the main form, you would just perform a frame swap.

Hiding some attributes i.e. textboxes, labels in parent form

m using a MDI child form and want it to display when a button from its parent form is clicked, at the same time i want that when the child form appears it should be placed such that some of the attributes of the parent form i.e. picture box is shown while others as labels and textboxes are hidden. Is this possible and how?
Not sure what language or framework you are using. And I don't get what you mean position so some attributes are hidden.
What I do know is that you should easily be able to hide those objects when you click the button, in C# for example myLabel.Visible = false; or you can use myLabel.Hide() and .Show()
You could put the controls, which should be always visible on a separate form and make it top-most, so that it always stays on top of every other form.
See this for an example: How to make a window stay on top

Merge tabs from child form into main form

I have page control in main form and page controls in child form , I
place the child form inside the main form by using docking features.
I am not using MDI style
Now both forms have tabs in page control and I need to merge the child form
tabs into the main form page control, what is the best way to do that?
The simplest and best way to handle multiple tabs on a page control is usually with Frames. Take the contents of each tab and factor them out into an individual frame for each tab. Move any shared components, state and code to a data module. Then just place those frames on the tab sheets of your page control.
This is a fairly simple approach that may or may not suit your needs.
For each child tab page you need to merge:
Create a tab in the main form page
control corresponding to the child
tab
Iterate over the Controls in the
child tab and change the Parent
property to the tab page you just
created
You do not need to deal with controls that are children of other controls. e.g. if you have a groupbox in your child tab, then changing it's parent will bring both it and all controls within it to the new parent.
If you need to be able to "unmerge" your tabs at any point then you will need to devise some mechanism to keep track of where the controls came from so you can restore the original Parent as/when needed.
To make things slightly easier you could place a TPanel in the child tabs, with it's Align property set to alClient. Place all the controls in the tab on that panel and then when you need to merge/unmerge you need only set the Parent of the panel control.
I just tried
procedure TForm1.Button1Click(Sender: TObject);
begin
while Form2.PageControl1.PageCount > 0 do
Form2.PageControl1.Pages[0].PageControl := PageControl1;
end;
and it worked fine. Am I missing something obvious, or why is everybody giving such sophisticated solutions? :-)

Resources