tags:

views:

287

answers:

3

Simple question. I have a panel with 3 components. All of them are have Align := alRight. But the problem is that during runtime the order of them is not the same as designtime.

Can I instead use alCustom to force the order at the right border ? I use D2007.

+5  A: 

Yes, you can do any type of custom alignment. Just use the control's OnAlignInsertBefore() and OnAlignPosition() events. (These exist in Delphi 2007, but aren't published so they don't appear in the Object Inspector's Events tab; you can still assign them in code. I've shown the prototypes below; they're documented in the help file under TAlignInsertBeforeEvent and TAlignPositionEvent; you can also see CustomAlignInsertBefore and OnAlignInsertBefore. )

TAlignInsertBeforeEvent = function(Sender: TWinControl; 
  C1, C2: TControl): Boolean of object;

TAlignPositionEvent = procedure(Sender: TWinControl; Control: TControl;
  var NewLeft, NewTop, NewWidth, NewHeight: Integer;
  var AlignRect: TRect; AlignInfo: TAlignInfo) of object;

The documentation contains pretty good descriptions of the parameters to both methods.

In Delphi 2010, these events are published and appear in the Events tab of the Object Inspector.

I've never seen the problem you're having, though... Did you try setting all of them to alNone, moving them away from the right edge, and then re-setting Align := alRight in the order you want them to appear?

Ken White
Oh! I've seen this happen plenty. It's especially daft when you have TSplitter instances that 'switch' to the far edge of the form, in effect disabling them. I've always solved this with extra TPanels and anchoring in the past.
Stijn Sanders
I have had to employ Ken's recommendation of setting the alignment to "None" and then setting them to "Right" in the order you want them to. I would like to add that you have to move them "far enough" from the right hand edge too. Say you have two components on a form: RightControl (aligned right) and NoneControl (aligned none). If NoneControl.Right > RightControl.Right, then changing NoneControl.align:=alRight will put it to the right of RightControl. For this reason I set them all to some negative number (e.g. in form create) and then set the alignment to right.
M Schenkel
+2  A: 

Have a look at Demo2 from this download.

Ulrich Gerhardt
+1  A: 

You don't reveal much of the problem at hand, but I would have taken a look at the flowpanel instead.

When dropping controls on a flowpanel, a new order-property 'automagically' appear. You can set which way your controls should flow, and if you want space between the controls, you set the margins on each control.

A little clearification: The new 'order-property' is actually called 'ControlIndex', and will appear at the bottom of the object inspector.

Vegar
Thanks, I have not tried yet but it seems to be the best solution (less coding).
Roland Bengtsson
Now I tried it and it didn't work. I added 3 buttons to a FlowPanel and I don't see any order property on the panel. Have I misunderstand you ?
Roland Bengtsson
I guess I was a little unclear. The 'order'-property is named 'ControlIndex' and will appear at the bottom of the object inspector.
Vegar
Ok, I found it. That was a good way to force a certain order for components. Until now I have used alLeft, alRight, alClient etc but that has a limit of number of components. Good!
Roland Bengtsson