Создать TToolbutton runtime
У меня проблема с созданием TToolbuttons
во время выполнения и как они появляются в моем TToolbar
,
По сути, я получил панель инструментов с несколькими кнопками. Я могу создавать кнопки во время выполнения и установить родительский элемент на панель инструментов. Но они всегда отображаются в качестве первых кнопок на моей панели инструментов.
Как я могу заставить их появиться в конце моей панели инструментов? Или любую позицию, которую я хочу, чтобы они были.
3 ответа
Вот общая процедура, которая берет панель инструментов и добавляет к ней кнопку с указанным заголовком:
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;
И вот пример использования этой процедуры:
procedure Button1Click(Sender: TObject);
begin
ToolBar1.ShowCaptions := True; //by default, this is False
AddButtonToToolbar(ToolBar1,IntToStr(ToolBar1.ButtonCount));
end;
Ваш вопрос также спрашивает, как добавить кнопку в произвольное место на панели TToolbar. Этот код похож на предыдущий, но он также позволяет вам указать, по какому индексу вы хотите, чтобы новая кнопка появлялась после.
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;
И вот пример использования для этой пересмотренной версии:
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;
Удачи!
Вы можете использовать left
собственность TToolButton
составная часть
проверьте этот образец
//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;
Если она работает как панель прокрутки, вы можете установить свойство.left на 1 больше, чем кнопка, чтобы разместить его слева от этой кнопки. Или установите свойство.left на 1 меньше, чем кнопка, чтобы разместить его справа от этой кнопки.