Create run time TTabItem , firemonkey

☆樱花仙子☆ 提交于 2019-12-01 08:54:34

问题


Seems like i need some help with a project. I have a routine , that constructs run time multiple TabItems on to a page control in firemonkey, and i want to have a close button on the tab. The new tab has a checkbox for the close button loading from the styler of the tabitems. The page has a default tab, and within some button, i am adding run time the new tab items. I have managed to apply the event for closing the default tab page, but doesn't work within the run time created tab pages. Any help would be appreciated.

This is the piece of code for the runtime tabitems

procedure TForm1.Button1Click(Sender: TObject);
var
  t : TTabItem;
  o : TFmxObject;
  i : Integer;
  c : TControl;
begin
  t := TTabItem.Create(pgeControl);

  t.Parent := pgeControl;

  o := FindBinding('imgCloseTabPage');
  if o<>nil then
  begin
    for i := 0 to ComponentCount - 1 do
    begin
      if Components[i] is TCheckBox then
      begin
        TCheckBox(Components[i]).OnClick := CheckBox1Click;
      end;
    end;
  end;

  if pgeControl.TabCount - 1 <= nTab then
  begin
    nTab := nTab + 1;
    t.Index := nTab
  end
  else
  begin
    t.Index := pgeControl.TabCount - 1;
    nTab := pgeControl.TabCount - 1;
  end;
  t.Tag := nTab;

  t.Text := 'Some text...' + ' ' + IntToStr(nTab);
  t.Name := 'tabPatient' + IntToStr(nTab);

  t.Height := 35;
  t.Width := 250;
  t.Margins.Top := 0;
  t.Margins.Left := 0;
  t.Margins.Bottom := 0;
  t.Margins.Right := 0;

  t.Padding.Top := -5;
  t.Padding.Left := 0;
  t.Padding.Bottom := 0;
  t.Padding.Right := 0;

  t.TextAlign := TTextAlign.taLeading;
  t.Width := (Length(t.Text) * 6 ) + 60;
  t.Font.Size := 15;
  t.StyleLookup := 'tabMainStyle1';

  l := TLayout.Create(t);
  l.Parent := t;
  l.Align := TAlignLayout.alClient;
  l.Margins.Top := -5;
  l.Margins.Left := 5;
  l.Margins.Right := 5;
  l.Margins.Bottom := 5;
  l.Padding.Top := 0;
  l.Padding.Left := 0;
  l.Padding.Bottom := 0;
  l.Padding.Right := 0;

  pgeControl.ActiveTab := pgeControl.Tabs[pgeControl.TabCount - 1];
end;

回答1:


You shoud call FindBinding after having applyed the custom style. Currently you call this before, so it can't find the object. Additionally there was a mistake when you was looking for the object.

so put this

o := t.FindBinding('imgCloseTabPage');
if o<>nil then
begin
  if o is TCheckBox then
    TCheckBox(o).OnClick := CheckBox1Click;
end;

after

t.StyleLookup := 'tabMainStyle1';

and the event should assigned.



来源:https://stackoverflow.com/questions/11134965/create-run-time-ttabitem-firemonkey

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!