astack
astack

Reputation: 161

Resizing buttons with keeping the same width when some buttons are invisible

I would like implement automatically resizing buttons keeping the same width when some buttons are invisible. I used code prepared by Andreas Rejbrand at this link, but the issue is more complicated when I set some buttons invisible. In places where we have invisible buttons there are gaps. My idea is to check how many buttons are invisible and next set btnWidth depending on the amount of visible buttons. I don't actually know how I can check if buttons are invisible in this case.

I want to use TAdvGlowButton component for the buttons and TPanel component for the panel and add OnResize procedure to panel like below:

procedure TForm3.Panel4Resize(Sender: TObject);
var
   i: Integer;
   btnWidth: Integer;
begin
   btnWidth := Panel4.Width div Panel4.ControlCount;
   for i := 0 to Panel4.ControlCount - 1  do
      begin
         Panel4.Controls[i].Left := i * btnWidth;
         Panel4.Controls[i].Width := btnWidth;
      end;
end;

Could you give me any idea how to solve this issue?

Upvotes: 1

Views: 360

Answers (1)

Hans
Hans

Reputation: 2262

procedure TForm3.Panel4Resize(Sender: TObject);
const
  cLeftMargin = 10;  //Margin at the left side of the group of buttons
  cSpacing = 10;     //Spacing/Margin between the buttons
  cRightMargin = 10; //Margin at the right side of the group of buttons
var
   i, VisibleControls, lLeft: Integer;
   btnWidth: Integer;
begin
   //count number of visible controls
   VisibleControls := 0;
   for i := 0 to Panel4.ControlCount - 1  do
      if Panel4.Controls[i].Visible then
         inc(VisibleControls);

   btnWidth := (Panel4.Width-cLeftMargin-cRightMargin - cSpacing*(VisibleControls-1)) div VisibleControls;

   //distribute the visible controls
   lLeft := cLeftMargin;
   for i := 0 to Panel4.ControlCount - 1  do
      if Panel4.Controls[i].Visible then
      begin
         Panel4.Controls[i].Left := lLeft;
         Panel4.Controls[i].Width := btnWidth;
         lLeft := lLeft + btnWidth + cSpacing;
      end;
end;

Upvotes: 1

Related Questions