Forum > Portuguese

Atribuir evento no onClick em tempo de execução

(1/1)

alessandrodidi:
Saudações developers!!!

Esta é a minha primeira postagem aqui no fórum.

A minha questão é a seguinte: estou desenvolvendo um sistema com modelagem TDI utilizando PageControl e para facilitar a reutilização de código desenvolvi uma procedure que fica em uma unit exclusiva para esta finalidade. Nesta unit eu criei uma classe para as TabSheets onde eu chamarei os forms. Até aí está funcionando bem, porém esbarrei em um problema, o botão fechar na aba. Como o Lazarus não permite utilizar o OwnerDraw no PageControl eu não consegui desenhar o botão fechar na aba, mas para contornar isso coloquei um Panel que fica na parte superior e da TabSheet e nele eu criei o botão fechar. Novamente, até aí funcionando bem.
O meu problema está na hora de atribuir a procedure para fechar a TabSheet no evento onClick do botão. Li algumas referências sobre o assunto, entretanto ainda não consegui uma forma de fazer funcionar.

Para facilitar estou postando o código


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---unit ExpertTabSheet; interface uses  Windows, Classes, ExtCtrls, Dialogs, ComCtrls, Forms, Buttons,  Controls, SysUtils, Themes, UxTheme, Graphics; type ETabSheet = Class (TTabSheet)  private   protected   public    constructor Create(AOwner: TComponent); override;    destructor Destroy; override;end;   procedure OpenWindowTab(PageControl: TPageControl; FormClass: TFormClass; MultiTab: Boolean);  procedure ClickEvent(Sender: TObject); implementation constructor ETabSheet.Create(AOwner:TComponent);begin  inherited Create(AOwner);end; destructor ETabSheet.Destroy;begin  inherited Destroy;end; procedure OpenWindowTab(PageControl: TPageControl; FormClass: TFormClass; MultiTab: Boolean);var  i: Integer;  Form: TForm;  TabSheet: ETabSheet;  ShiftOn: TShiftState;  PPointer: TPoint;  btnClose: TSpeedButton;  TitleBar, ButtonsSet: TPanel;begin  try    //Cria a TabSheet    TabSheet := ETabSheet.Create(PageControl);     //Verifica se o form já foi criado    Form := FormClass.Create(TabSheet);    for i := 0 to PageControl.ControlCount -1 do    if (PageControl.Pages[i].Caption = Form.Caption) and (MultiTab = False) then      begin        PageControl.ActivePageIndex := i;        exit;      end;     //Cria o Form    TabSheet.PageControl := PageControl;    Form := FormClass.Create(TabSheet);    Form.Parent := TabSheet;     //Cria uma barra de títulos para a TabSheet    TitleBar := TPanel.Create(TabSheet);    TitleBar.Parent := TabSheet;    TitleBar.Align := alTop;    TitleBar.Height := 22;    TitleBar.Caption := Form.Caption;    TitleBar.Color := clWindow;    TitleBar.Visible := True;     //Propriedade do Form    Form.Align := alClient;    Form.BorderStyle := bsNone;    Form.Visible := True;    TabSheet.Caption := Form.Caption;     //Cria um container para os botões    ButtonsSet := TPanel.Create(TitleBar);    ButtonsSet.Parent := TitleBar;    ButtonsSet.Align := alRight;    ButtonsSet.Caption := '';    ButtonsSet.Width := 80;    ButtonsSet.BorderSpacing.Right := 1;    ButtonsSet.BiDiMode := bdRightToLeft;    ButtonsSet.BorderStyle := bsNone;    ButtonsSet.BevelInner := bvNone;    ButtonsSet.BevelOuter := bvNone;    ButtonsSet.Anchors := [akTop,akRight];    ButtonsSet.Color := clNone;    ButtonsSet.Visible := True;     //Cria o botão para fechar a TabSheet    btnClose := TSpeedButton.Create(ButtonsSet);    btnClose.Parent := ButtonsSet;    btnClose.Width := 19;    btnClose.Height := 19;    btnClose.Left := 60;    btnClose.BorderSpacing.Around := 1;    btnClose.BorderSpacing.InnerBorder := 0;    btnClose.BorderSpacing.CellAlignHorizontal := ccaCenter;    btnClose.BorderSpacing.CellAlignVertical := ccaCenter;    btnClose.Align := alNone;    btnClose.Anchors := [akTop,akRight];    btnClose.Caption := 'X';    btnClose.Hint := 'Fechar aba';    btnClose.Transparent := True;    btnClose.ShowHint := True;    btnClose.Visible := True;    btnClose.OnClick := @ClickEvent;     //Coloca o foco na TabSheet aberta    PageControl.ActivePage := TabSheet;    Form.SetFocus;  except    Form.Free;  end;end; procedure ClickEvent(Sender: TObject);begin  ShowMessage('Aqui fica a rotina para destruir a TabSheet e o Form');end;  initialization  ETabSheet.ClassName; end. 

lucamar:
Excluir uma TTabSheet é tão simples quanto isto:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure CloseTab(const ATab: TTabSheet);begin  if Assigned(ATab) then begin    {Se houver controles cujo proprietário (Owner)     não seja o TabSheet, faça Free aqui}    ATab.Free;  end;end;
Traduzido pelo Google ;)

alessandrodidi:
Ótimo lucamar, obrigado pela contribuição.
Mais uma questão: para eu atribuir essa procedure no evento onclick do botão fechar, basta eu atribui-la, tipo

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---...BotaoFechar.onClick := @CloseTab(minhaTabSheet);...
Seria somente isso ou há mais algum cuidado que devo tomar para poder chamar essa procedure no botão fechar?




--- Quote from: lucamar on July 22, 2021, 02:03:45 am ---Excluir uma TTabSheet é tão simples quanto isto:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure CloseTab(const ATab: TTabSheet);begin  if Assigned(ATab) then begin    {Se houver controles cujo proprietário (Owner)     não seja o TabSheet, faça Free aqui}    ATab.Free;  end;end;
Traduzido pelo Google ;)

--- End quote ---

pauloh0:
Olá se entendi bem é um clique em Runtime que voce precisa:

BitBtn2Click(self);
ou
BitBtn2Click(nil);

Navigation

[0] Message Index

Go to full version