If I understand your problem correctly you want to hide toolbuttons, but keep the order of the visible buttons. When you turn the hidden buttons on again you expect them to be at their old places.
As I found out in my experiments, the alCustom method proposed above does not work properly once I had rearranged the buttons at design time.
Maybe this procedure helps you:
type
TMyToolbutton = class(TToolButton);
procedure SetToolButtonIndex(AToolbutton: TToolButton; Value: Integer);
var
btns: array of TToolbutton;
i, n, currentIdx : integer;
toolbar: TToolbar;
begin
if not AToolbutton.Visible then
exit;
currentIdx := AToolButton.Index;
toolbar := TMyToolbutton(AToolButton).FToolbar;
// Create a separate list of current toolbuttons, invisible ones marked by nil
SetLength(btns, toolbar.ButtonCount);
for i:=0 to toolbar.ButtonCount-1 do
if toolbar.Buttons[i].Visible then
btns[i] := toolbar.Buttons[i]
else
btns[i] := nil;
// Swap "AToolButton" with the button currently at index "Value"
btns[currentIdx] := btns[Value];
btns[Value] := AToolButton;
// Now, going from the last to the first button, move each visible button to
// the left of the toolbar. This reorders the buttons in the requested order.
for i:=Length(btns)-1 downto 0 do
if btns[i] <> nil then
btns[i].Left := -1;
end;
The basic trick is to create a list of the visible toolbuttons in the requested order, and then, beginning at the end of the list, move each button beyond the left edge of the toolbar which makes this button the first one. After this loop is finished, all visible buttons will be in their correct order.
Call this procedure for all toolbuttons whenever you show/hide toolbuttons, like here:
procedure TForm1.CheckBox1Change(Sender: TObject);
begin
Toolbutton1.Visible := Checkbox1.Checked;
Toolbutton3.Visible := Checkbox1.Checked;
Toolbutton5.Visible := Checkbox1.Checked;
SetToolButtonIndex(Toolbutton1, 0);
SetToolButtonIndex(Toolbutton2, 1);
SetToolButtonIndex(Toolbutton3, 2);
SetToolButtonIndex(Toolbutton4, 3);
SetToolButtonIndex(Toolbutton5, 4);
SetToolButtonIndex(Toolbutton6, 5);
SetToolButtonIndex(Toolbutton7, 6);
end;