开发者

Create TToolbutton runtime

开发者 https://www.devze.com 2023-01-23 23:42 出处:网络
I have a problem with creating TToolbuttons at runtime and how they appear in my TToolbar. Basically I got a toolbar with some buttons in it already. I can createbuttons at runtime

I have a problem with creating TToolbuttons at runtime and how they appear in my TToolbar.

Basically I got a toolbar with some buttons in it already. I can create buttons at runtime and set the parent to the toolbar. But they are always shown as the first buttons in my tool开发者_Go百科bar.

How can I make them appear at the end of my toolbar? Or any position I want them to be.


Here is a generic procedure that takes a toolbar, and adds a button to it, with a specified caption:

procedure AddButtonToToolbar(var bar: TToolBar; caption: string);
var
  newbtn: TToolButton;
  lastbtnidx: integer;
begin
  newbtn := TToolButton.Create(bar);
  newbtn.Caption := caption;
  lastbtnidx := bar.ButtonCount - 1;
  if lastbtnidx > -1 then
    newbtn.Left := bar.Buttons[lastbtnidx].Left + bar.Buttons[lastbtnidx].Width
  else
    newbtn.Left := 0;
  newbtn.Parent := bar;
end;

And here is example usage of that procedure:

procedure Button1Click(Sender: TObject);
begin
  ToolBar1.ShowCaptions := True;  //by default, this is False
  AddButtonToToolbar(ToolBar1,IntToStr(ToolBar1.ButtonCount));
end;

Your question does also ask how to add a button to an arbitrary place on the TToolbar. This code is similar to before, but it also allows you to specify which index you want the new button to appear after.

procedure AddButtonToToolbar(var bar: TToolBar; caption: string;
  addafteridx: integer = -1);
var
  newbtn: TToolButton;
  prevBtnIdx: integer;
begin
  newbtn := TToolButton.Create(bar);
  newbtn.Caption := caption;

  //if they asked us to add it after a specific location, then do so
  //otherwise, just add it to the end (after the last existing button)
  if addafteridx = -1 then begin
    prevBtnIdx := bar.ButtonCount - 1;
  end
  else begin
    if bar.ButtonCount <= addafteridx then begin
      //if the index they want to be *after* does not exist,
      //just add to the end
      prevBtnIdx := bar.ButtonCount - 1;
    end
    else begin
      prevBtnIdx := addafteridx;
    end;
  end;

  if prevBtnIdx > -1 then
    newbtn.Left := bar.Buttons[prevBtnIdx].Left + bar.Buttons[prevBtnIdx].Width
  else
    newbtn.Left := 0;

  newbtn.Parent := bar;
end;

And here is example usage for this revised version:

procedure Button1Click(Sender: TObject);
begin
  //by default, "ShowCaptions" is false
  ToolBar1.ShowCaptions := True;

  //in this example, always add our new button immediately after the 0th button
  AddButtonToToolbar(ToolBar1,IntToStr(ToolBar1.ButtonCount),0);
end;

Good luck!


You can use the left property of the TToolButton component

check this sample

//adding buttons to the end  of the ToolBar.
procedure TForm1.Button1Click(Sender: TObject);
var
 Toolbutton : TToolButton;
begin
   Toolbutton :=TToolButton.Create(ToolBar1);
   Toolbutton.Parent  := ToolBar1;
   Toolbutton.Caption := IntToStr(ToolBar1.ButtonCount);
   Toolbutton.Left    := ToolBar1.Buttons[ToolBar1.ButtonCount-1].Left + ToolBar1.ButtonWidth;
end;


If it works like the Scroll Panel then you can set the .left property to be 1 more than a button to place it to the left of that button. Or set the .left property to be 1 less than the button to place it to the right of that button.

0

精彩评论

暂无评论...
验证码 换一张
取 消