Is there an option in Delphi to keep the Form controls in the same order as the DFM?

452 views Asked by At

I just created a new VCL application and placed the following controls on the form in this order:

  1. MainMenu
  2. ToolBar
  3. StatusBar

The controls appear in the TForm class in the order I added them to the form. When I compare the TForm class to the DFM the controls are in a different order.

Class:

type
  TForm5 = class(TForm)
    MainMenu1: TMainMenu;
    ToolBar1: TToolBar;
    StatusBar1: TStatusBar;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

DFM (properties removed for illustration purposes)

object Form5: TForm5
  object ToolBar1: TToolBar
  end
  object StatusBar1: TStatusBar
  end
  object MainMenu1: TMainMenu
  end
end

I'd like to order the controls in the class definition so they match the DFM file. Is there an option within the IDE to do this?

1

There are 1 answers

0
Deltics On

As noted in the comments to the question, there is no such mechanism in the standard IDE and the utility of any such mechanism (or outcome) is dubious and limited at the very best.

But if you absolutely must have some sort of order/organization in this area and do not wish to have to arrange these declarations manually, then I would simply use the GExperts "Sort source lines" editor tool periodically, as required to maintain the order/grouping you desire.

Select the declarations you wish to sort and sort ascending or descending according to preference.

As long as you have consistently named all your components this will give you any 'grouping' you desire, as long as you enforce that by a sort order embodied in your component naming convention. e.g. all menu items begin "mi...", all File menu items then beginning with "miFile..." etc etc.

However, you are then at the mercy of your names to determine the resulting order. For example a hypothetical set of File menu items will end up as:

miFileExit
miFileNew
miFileOpen
miFilePrint
miFileSave
miFileSeparator1
miFileSeparator2

Which is almost certainly not the order in which they appear in the menu itself. But why this should matter is not clear and, as a way to facilitate the location of declarations, an alpha sort is most likely to be useful to a human who doesn't have a copy of the persisted form component declaration order in their head.