Stick to window?

I have an application, which creates additional forms. How can I make these additional forms stick to the main application window (right side). I want the top left of the modal form to stick to the top right of the main application form. when the main application moves, the other form should move with it, and visa versa

thanks
DelphiRulezAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Emmanuel PASQUIERFreelance Project ManagerCommented:
there might be other ways with not too old Delphi version, but one that would work always is to act on the onShow event of your subforms, and onMove of your main form (which is not exposed but can be)

about the onMove, if your other form are MODAL, you can't interact with the main form while those are open, so you don't have to bother with that and only need onShow
//=======================================================================
//== this should be put in a tool unit, as it will be used by many forms 

unit StickTools;

interface

Uses Form;

procedure StickToTopRight(formToStick,Mainform:TForm);

implementation

procedure StickToTopRight(formToStick,Mainform:TForm);
begin
 formToStick.Left:=Mainform.Left+Mainform.Width;
 formToStick.Top:=Mainform.Top;
end;

end.

//=======================================================================
// main form unit

Unit MainForm;

interface

type
 TForm1=class(TForm)
 protected
  procedure OnMove(var Msg: TWMMove); message WM_MOVE;
 end;

implementation

uses Unitform2,Unitform3, StickTools;

procedure TForm1.OnMove(var Msg: TWMMove);
begin
 inherited;
 if Assigned(form2) Then StickToTopRight(form2,self);
 if Assigned(form3) Then StickToTopRight(form3,self);
end

//=======================================================================
// modal form unit

type
 TForm2=class(TForm)
  procedure FormShow(Sender: TObject);
 public
//...
 end;

implementation

Uses StickTools, MainForm;

procedure TForm2.FormShow(Sender: TObject);
begin
 StickToTopRight(self, form1); 
end;

Open in new window

0
DelphiRulezAuthor Commented:
1.) When I run my application and display the secondary form. It comes up in the middle of the screen (do to the fact that position is set to poScreenCenter. It does not attach to the main form. Only when i move the main form, does it attach.

2.) Works if I move the main form, but not the secondary form. Can i make the main form move with the secondary form when the secondary form is moved?

3.) How do i make it work for modal forms as well

thanks

0
Emmanuel PASQUIERFreelance Project ManagerCommented:
1) don't use poScreenCenter but poDesigned      
that way, the StickToTopRight call in FormShow will apply and not be override by poScreenCenter

2) create a StickToTopLeft procedure in StickTools
procedure StickToTopLeft(formToStick,Mainform:TForm);
begin
 formToStick.Left:=Mainform.Left-formToStick.Width;
 formToStick.Top:=Mainform.Top;
end;

declare onMove also on secondary form
procedure TForm2.OnMove(var Msg: TWMMove);  // message WM_MOVE;
begin
 StickToTopLeft(form1, Self);
end;

3) You just can't move a form (or click or input key or whatever act on it) when another is displayed above it with ShowModal
0
DelphiRulezAuthor Commented:
there is a problem somwheres. when i implement your code, my program runs, but my main form is hidden off the screen.

if i disabled the two lines below

procedure StickToTopLeft(formToStick,Mainform:TForm);
begin
 //formToStick.Left:=Mainform.Left-Mainform.Width;
 //formToStick.Top:=Mainform.Top;
end;

, then the main form is visable again, and works like before

i.e. move the main form and the secondary form moves with it
0
Emmanuel PASQUIERFreelance Project ManagerCommented:
I should have thought bout that, that's probably a typical case of events cross-talk.
event A is doing something that triggers event B that's doing somthing that should trigger A... until something really wrong happen.

in your case, when you show or move a form it wants to reposition the other form, then removes the first form...

solution in these case : disable the mechanism before doing the treatment, and reenable after
unit StickTools;

interface

Uses Form;

procedure StickToTopRight(formToStick,Mainform:TForm);

implementation

Const
 DisableStick:Boolean=False;

procedure StickToTopRight(formToStick,Mainform:TForm);
begin
 if DisableStick Then Exit;
 DisableStick:=True;
 formToStick.Left:=Mainform.Left+Mainform.Width;
 formToStick.Top:=Mainform.Top;
 DisableStick:=False;
end;

procedure StickToTopLeft(formToStick,Mainform:TForm);
begin
 if DisableStick Then Exit;
 DisableStick:=True;
 formToStick.Left:=Mainform.Left-formToStick.Width;
 formToStick.Top:=Mainform.Top;
 DisableStick:=False;
end;

end.

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.