Criando Objetos conforme dados em Banco

Delphi

23/03/2011

Pessoal, tenho um sistema de controle de Park infantil, onde abro o tempo da criança e quando essa criança sai, o sistema calcula quanto tempo ela ficou e quanto tem a pagar.   O sistema funciona perfeitamente, porém eu estou com problema de recursos do sistema e na atualização dos códigos.   Para cada tela referente a cada criança, eu tenho um TTimer q faz a contagem de tempo e o cálculo de uso, fica registrado no banco, o
Id da criança, Data de entrada, Hora de entrada. Quando fecha o tempo, grava data de saída, hora de saída, tempo utilizado, valor a pagar e registra que a tela está desocupada.   Daí queria saber o seguinte, ao invez deu recriar os códigos para cada
tela, tipo, eu tenho 28 telas e utilizo 28 TTimers, 28 botões pra abrir e fechar tela.   Queria tipo assim, uma tabela temporária, para cada registro nessa tabela, o sistema criar automaticamente um TTimer, o botão abre e fecha tempo.     Exemplo:   IdCrian 01   Timer01 btAbre01 btFecha01 ---------------------- IdCrian 02 Timer02 btAbre02 btFecha02   Ou seja, eu só irei escrever o código uma únicavez para criar os objetos com base nos dados na table temporária.   Espero ter explicado corretamente minha idéia...
Paulo Andrade

Paulo Andrade

Curtidas 0

Respostas

Gustavo Bretas

Gustavo Bretas

23/03/2011

E ae Paulo, tudo certo?   Rapaz, se eu entendi o que vc fez, deve estar sofrendo bastante com esse negocio aí!   Tenta fazer o seguinte, cria um formulário para mostrar o tempo das crianças, cria os métodos para carregar as informações da tabela na tela tudo certinho!   Depois disso pronto, vc pode fazer um campo de pesquisa onde vai pesquisar a criança, e um botão que vai abrir a tela, no evento do botão vc pode fazer:  
 
with TFormularioCriado.Create(Application) do
try
  mtdCarregaInf(CodigoDaCriança);
  ShowModal;
finally
  Free
end;
  Usando with TFormularioCriado.Create(Application) do, vc vai criar o formulário sem precisar de uma variável do tipo de formulário, se vc usar showmodal tranquilo, senão vc vai ter problema para liberar o formulário da memória.   Se preferir, vc pode ter uma tela de consulta com uma grande com todas as crianças que estão usando o parque, e para ver o tempo de cada uma, ou fazer o check-out, usar essa tela!   Pensa ae, abraço!
GOSTEI 0
Paulo Andrade

Paulo Andrade

23/03/2011

E ae Paulo, tudo certo?   Rapaz, se eu entendi o que vc fez, deve estar sofrendo bastante com esse negocio aí!   Tenta fazer o seguinte, cria um formulário para mostrar o tempo das crianças, cria os métodos para carregar as informações da tabela na tela tudo certinho!   Depois disso pronto, vc pode fazer um campo de pesquisa onde vai pesquisar a criança, e um botão que vai abrir a tela, no evento do botão vc pode fazer:  
 
with TFormularioCriado.Create(Application) do
try
  mtdCarregaInf(CodigoDaCriança);
  ShowModal;
finally
  Free
end;
  Usando with TFormularioCriado.Create(Application) do, vc vai criar o formulário sem precisar de uma variável do tipo de formulário, se vc usar showmodal tranquilo, senão vc vai ter problema para liberar o formulário da memória.   Se preferir, vc pode ter uma tela de consulta com uma grande com todas as crianças que estão usando o parque, e para ver o tempo de cada uma, ou fazer o check-out, usar essa tela!   Pensa ae, abraço!
.. . .   O que realmente quero é o seguinte, para cada Registro na tabela temporária, o sistema crie automaticamente, um no TTimer e um Novo Botão.   Registro 1 Timer1 Botao1   Resitro 2 Timer2 Botao2...
GOSTEI 0
Gustavo Bretas

Gustavo Bretas

23/03/2011

É rapaz, ta complicado entender o que vc quer! Não estou enchergando a necessidade de cada registro ter um Timer e no caso do Botão vc vai dar trabalho para alinha-los! De uma olhada array dinamico de objetos.   Segue um exemplo:  
 
unit untPrincipal;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, DB, DBClient, ComCtrls, ToolWin;
type
  TfrmPrincipal = class(TForm)
    Timer: TTimer;
    btnAdicionar: TButton;
    procedure btnAdicionarClick(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
  private
    nArrayButton : Array of Array of String;
    { Private declarations }
  public
    { Public declarations }
  end;
var
  frmPrincipal: TfrmPrincipal;
implementation
uses DateUtils;
{$R *.dfm}
procedure TfrmPrincipal.btnAdicionarClick(Sender: TObject);
const
  cSeparador : Integer = 4;
var
  nLeft, nTop : Integer;
begin
  if High(nArrayButton) < 0 then
  begin
    nLeft := 10;
    nTop := 60;
    SetLength(nArrayButton, 1);
    SetLength(nArrayButton[0], 2);
  end
  else
  begin
    nLeft := TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Left
      + TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Width + cSeparador;
    nTop := TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Top;
    if TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Width + nLeft >= Self.Width then
    begin
      nLeft := 10;
      nTop := TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Top
        + TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Height + cSeparador;
    end;
    SetLength(nArrayButton, High(nArrayButton) + 2);
    SetLength(nArrayButton[High(nArrayButton)], High(nArrayButton) + 1);
  end;
  with TButton.Create(Self) do
  begin
    Left := nLeft;
    Top := nTop;
    Parent := Self;
    Name := 'btn'+ IntToStr(High(nArrayButton));
    Caption := 'btn'+ IntToStr(High(nArrayButton));
    nArrayButton[High(nArrayButton), 0] := Name;
    nArrayButton[High(nArrayButton), 1] := FormatDateTime('dd/mm/yyyy hh:mm:ss', Now);
    Application.ProcessMessages;
  end;
end;
procedure TfrmPrincipal.TimerTimer(Sender: TObject);
var
  i : Integer;
begin
  for i := Low(nArrayButton) to High(nArrayButton) do
    with TButton(Self.FindComponent(nArrayButton[i, 0])) do
    begin
      Caption := FormatDateTime('hh:mm:ss', Now - StrToDateTime(nArrayButton[i, 1]));
    end;
end;
end.
  No formulário tem apenas um botão e um Time, veja se é isso que vc quer e adeque para sua necessidade!   Abraço!
GOSTEI 0
Paulo Andrade

Paulo Andrade

23/03/2011

É rapaz, ta complicado entender o que vc quer! Não estou enchergando a necessidade de cada registro ter um Timer e no caso do Botão vc vai dar trabalho para alinha-los! De uma olhada array dinamico de objetos.   Segue um exemplo:  
 
unit untPrincipal;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, DB, DBClient, ComCtrls, ToolWin;
type
  TfrmPrincipal = class(TForm)
    Timer: TTimer;
    btnAdicionar: TButton;
    procedure btnAdicionarClick(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
  private
    nArrayButton : Array of Array of String;
    { Private declarations }
  public
    { Public declarations }
  end;
var
  frmPrincipal: TfrmPrincipal;
implementation
uses DateUtils;
{$R *.dfm}
procedure TfrmPrincipal.btnAdicionarClick(Sender: TObject);
const
  cSeparador : Integer = 4;
var
  nLeft, nTop : Integer;
begin
  if High(nArrayButton) < 0 then
  begin
    nLeft := 10;
    nTop := 60;
    SetLength(nArrayButton, 1);
    SetLength(nArrayButton[0], 2);
  end
  else
  begin
    nLeft := TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Left
      + TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Width + cSeparador;
    nTop := TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Top;
    if TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Width + nLeft >= Self.Width then
    begin
      nLeft := 10;
      nTop := TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Top
        + TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Height + cSeparador;
    end;
    SetLength(nArrayButton, High(nArrayButton) + 2);
    SetLength(nArrayButton[High(nArrayButton)], High(nArrayButton) + 1);
  end;
  with TButton.Create(Self) do
  begin
    Left := nLeft;
    Top := nTop;
    Parent := Self;
    Name := 'btn'+ IntToStr(High(nArrayButton));
    Caption := 'btn'+ IntToStr(High(nArrayButton));
    nArrayButton[High(nArrayButton), 0] := Name;
    nArrayButton[High(nArrayButton), 1] := FormatDateTime('dd/mm/yyyy hh:mm:ss', Now);
    Application.ProcessMessages;
  end;
end;
procedure TfrmPrincipal.TimerTimer(Sender: TObject);
var
  i : Integer;
begin
  for i := Low(nArrayButton) to High(nArrayButton) do
    with TButton(Self.FindComponent(nArrayButton[i, 0])) do
    begin
      Caption := FormatDateTime('hh:mm:ss', Now - StrToDateTime(nArrayButton[i, 1]));
    end;
end;
end.
  No formulário tem apenas um botão e um Time, veja se é isso que vc quer e adeque para sua necessidade!   Abraço!
. . . Para fácil entendimento, o que quero saber é como criar objetos baseado nos dados de uma tabela.   Por exemplo, digamos, numa DBGrid tenha três linhas de 3 crianças, quero que apareça no a foto de cada criança, a hora de entrada, quanto tempo tem de uso, quanto tem a pagar.   Que isso seja mostrando dentro de uma TPanel.   Acho q deu pra explicar rsrsrs.   Meu sistema funiona, mas para cada criança eu tenho um Timer diferente.      
GOSTEI 0
Paulo Andrade

Paulo Andrade

23/03/2011

É rapaz, ta complicado entender o que vc quer! Não estou enchergando a necessidade de cada registro ter um Timer e no caso do Botão vc vai dar trabalho para alinha-los! De uma olhada array dinamico de objetos.   Segue um exemplo:  
 
unit untPrincipal;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls, StdCtrls, DB, DBClient, ComCtrls, ToolWin;
type
  TfrmPrincipal = class(TForm)
    Timer: TTimer;
    btnAdicionar: TButton;
    procedure btnAdicionarClick(Sender: TObject);
    procedure TimerTimer(Sender: TObject);
  private
    nArrayButton : Array of Array of String;
    { Private declarations }
  public
    { Public declarations }
  end;
var
  frmPrincipal: TfrmPrincipal;
implementation
uses DateUtils;
{$R *.dfm}
procedure TfrmPrincipal.btnAdicionarClick(Sender: TObject);
const
  cSeparador : Integer = 4;
var
  nLeft, nTop : Integer;
begin
  if High(nArrayButton) < 0 then
  begin
    nLeft := 10;
    nTop := 60;
    SetLength(nArrayButton, 1);
    SetLength(nArrayButton[0], 2);
  end
  else
  begin
    nLeft := TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Left
      + TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Width + cSeparador;
    nTop := TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Top;
    if TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Width + nLeft >= Self.Width then
    begin
      nLeft := 10;
      nTop := TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Top
        + TButton(Self.FindComponent('btn'+ IntToStr(High(nArrayButton)))).Height + cSeparador;
    end;
    SetLength(nArrayButton, High(nArrayButton) + 2);
    SetLength(nArrayButton[High(nArrayButton)], High(nArrayButton) + 1);
  end;
  with TButton.Create(Self) do
  begin
    Left := nLeft;
    Top := nTop;
    Parent := Self;
    Name := 'btn'+ IntToStr(High(nArrayButton));
    Caption := 'btn'+ IntToStr(High(nArrayButton));
    nArrayButton[High(nArrayButton), 0] := Name;
    nArrayButton[High(nArrayButton), 1] := FormatDateTime('dd/mm/yyyy hh:mm:ss', Now);
    Application.ProcessMessages;
  end;
end;
procedure TfrmPrincipal.TimerTimer(Sender: TObject);
var
  i : Integer;
begin
  for i := Low(nArrayButton) to High(nArrayButton) do
    with TButton(Self.FindComponent(nArrayButton[i, 0])) do
    begin
      Caption := FormatDateTime('hh:mm:ss', Now - StrToDateTime(nArrayButton[i, 1]));
    end;
end;
end.
  No formulário tem apenas um botão e um Time, veja se é isso que vc quer e adeque para sua necessidade!   Abraço!
. . . Sua Colocação já abriu meu entendimento, acho q talvez der certo o que eu preciso, vou verificar...   Agora, como posso criar um código dentro dos novos botões criados?
GOSTEI 0
POSTAR