Fórum Componentes em tempo de execução. #390838

19/11/2010

0



Frederico Brigatte***

Frederico Brigatte***

Responder

Posts

19/11/2010

Frederico Brigatte***



[/qGostaria de saber se existe alguma maneira de automatizar a criação de componentes em tempo de execução.

Por exemplo. Fiz um código que cria 30 botões em tempo de execução utilizando For.

Utilizo esse código para definir a posição do botão:

Quando for menor que 10:

Left := 10;
Top := 30 + x * 30;

Quando for maior que 20:

Left := 100;
Top := 1 + (x - 9) * 30;

Quando for maior que 30:

Left := 190;
Top := 1 + (x - 19) * 30;

Foto em anexo.

Gostaria de automatizar o left e o  top.uote]
Responder

Gostei + 0

19/11/2010

Frederico Brigatte***



[/qGostaria de saber se existe alguma maneira de automatizar a criação de componentes em tempo de execução.

Por exemplo. Fiz um código que cria 30 botões em tempo de execução utilizando For.

Utilizo esse código para definir a posição do botão:

Quando for menor que 10:

Left := 10;
Top := 30 + x * 30;

Quando for maior que 20:

Left := 100;
Top := 1 + (x - 9) * 30;

Quando for maior que 30:

Left := 190;
Top := 1 + (x - 19) * 30;

Foto em anexo.

Gostaria de automatizar o left e o  top.uote]


frederico.brigatte@itelefonica.com.br
Responder

Gostei + 0

19/11/2010

Wilson Junior

Teste assim
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x: integer;
  Botao: TButton;
begin
  for x := 0 to 29 do
  begin
    Botao := TButton.Create( Self );
    Botao.Parent := Self;
    Botao.Caption := 'Botão ' + IntToStr( x + 1 );

    Botao.Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    Botao.Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );
  end;
end;


Espero ter colaborado.
Responder

Gostei + 0

19/11/2010

Frederico Brigatte***

Teste assim
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x: integer;
  Botao: TButton;
begin
  for x := 0 to 29 do
  begin
    Botao := TButton.Create( Self );
    Botao.Parent := Self;
    Botao.Caption := 'Botão ' + IntToStr( x + 1 );

    Botao.Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    Botao.Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );
  end;
end;


Espero ter colaborado.


Estou testando aqui. Esse Const coloco depois do Implementation?

O código que uso é esse:

procedure TForm1.Button1Click(Sender: TObject);
var
 x, x1, x2 : integer;
begin
for x := 1 to 30 do begin   // Cria botões de 1 a 10
 ArrayEdit[x] := TButton.Create(Self);
 ArrayEdit[x].Parent := Self;
 ArrayEdit[x].Caption := 'Mesa ' + IntToStr(x);
 ArrayEdit[x].OnMouseMove :=    ControlMouseMove;
 ArrayEdit[x].OnMouseDown :=    ControlMouseDown;
 ArrayEdit[x].OnMouseUp   :=    ControlMouseUp;

 if x <= 10 then
 Begin
  ArrayEdit[x].Left := 10;
  ArrayEdit[x].Top := 30 + x * 30;
 end;

 if x > 10 then
 Begin
  ArrayEdit[x].Left := 100;
  ArrayEdit[x].Top := 1 + (x - 9) * 30;
 end;

 if x > 20 then
 Begin
  ArrayEdit[x].Left := 190;
  ArrayEdit[x].Top := 1 + (x - 19) * 30;
 end;


end;

Responder

Gostei + 0

19/11/2010

Frederico Brigatte***

Teste assim
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x: integer;
  Botao: TButton;
begin
  for x := 0 to 29 do
  begin
    Botao := TButton.Create( Self );
    Botao.Parent := Self;
    Botao.Caption := 'Botão ' + IntToStr( x + 1 );

    Botao.Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    Botao.Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );
  end;
end;


Espero ter colaborado.


Estou testando aqui. Esse Const coloco depois do Implementation?



Mais uma dúvida, caso queira colocar uma imagem no botão, é possível? Poderia comentarexplicar como foi feito e caso queira preencher o form todo sem ter a barra de rolagem, tbém é possível?

Responder

Gostei + 0

19/11/2010

Wilson Junior

Deve ser utilizado o TBitBtn para ter imagem.
Teste assim:
procedure TForm1.Button1Click(Sender: TObject);
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x: integer;
  Botao: TButton;
begin
  for x := 0 to 29 do
  begin
    ArrayEdit[x] := TBitBtn.Create( Self );
    ArrayEdit[x].Parent := Self;
    ArrayEdit[x].Caption := 'Mesa ' + IntToStr( x );
    ArrayEdit[x].OnMouseMove := ControlMouseMove;
    ArrayEdit[x].OnMouseDown := ControlMouseDown;
    ArrayEdit[x].OnMouseUp   := ControlMouseUp;
    ArrayEdit[x].Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    ArrayEdit[x].Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );

    ArrayEdit[x].Glyph.LoadFromFile( 'C:\MinhaImagem' + IntToStr(x) + '.bmp' );
  end;
end;


Espero ter colaborado.
Responder

Gostei + 0

19/11/2010

Frederico Brigatte***

Deve ser utilizado o TBitBtn para ter imagem.
Teste assim:
procedure TForm1.Button1Click(Sender: TObject);
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x: integer;
  Botao: TButton;
begin
  for x := 0 to 29 do
  begin
    ArrayEdit[x] := TBitBtn.Create( Self );
    ArrayEdit[x].Parent := Self;
    ArrayEdit[x].Caption := 'Mesa ' + IntToStr( x );
    ArrayEdit[x].OnMouseMove := ControlMouseMove;
    ArrayEdit[x].OnMouseDown := ControlMouseDown;
    ArrayEdit[x].OnMouseUp   := ControlMouseUp;
    ArrayEdit[x].Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    ArrayEdit[x].Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );

    ArrayEdit[x].Glyph.LoadFromFile( 'C:\MinhaImagem' + IntToStr(x) + '.bmp' );
  end;
end;


Espero ter colaborado.


Ok, e se quiser o form todo com botão sem que apareça as barras de rolagens?
Responder

Gostei + 0

19/11/2010

Frederico Brigatte***

Deve ser utilizado o TBitBtn para ter imagem.
Teste assim:
procedure TForm1.Button1Click(Sender: TObject);
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x: integer;
  Botao: TButton;
begin
  for x := 0 to 29 do
  begin
    ArrayEdit[x] := TBitBtn.Create( Self );
    ArrayEdit[x].Parent := Self;
    ArrayEdit[x].Caption := 'Mesa ' + IntToStr( x );
    ArrayEdit[x].OnMouseMove := ControlMouseMove;
    ArrayEdit[x].OnMouseDown := ControlMouseDown;
    ArrayEdit[x].OnMouseUp   := ControlMouseUp;
    ArrayEdit[x].Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    ArrayEdit[x].Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );

    ArrayEdit[x].Glyph.LoadFromFile( 'C:\MinhaImagem' + IntToStr(x) + '.bmp' );
  end;
end;


Espero ter colaborado.


Ok, e se quiser o form todo com botão sem que apareça as barras de rolagens?


E se quiser o form todo e sem que apareça as barras re rolagem horizontal e vertical. Que redimensione o form automático.

Está quase lá.
Responder

Gostei + 0

19/11/2010

Wilson Junior

Tente assim
procedure TForm1.Button1Click(Sender: TObject);
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x: integer;
  Botao: TButton;
begin
  for x := 0 to 29 do
  begin
    ArrayEdit[x] := TBitBtn.Create( Self );
    ArrayEdit[x].Parent := Self;
    ArrayEdit[x].Caption := 'Mesa ' + IntToStr( x );
    ArrayEdit[x].OnMouseMove := ControlMouseMove;
    ArrayEdit[x].OnMouseDown := ControlMouseDown;
    ArrayEdit[x].OnMouseUp   := ControlMouseUp;
    ArrayEdit[x].Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    ArrayEdit[x].Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );

    ArrayEdit[x].Glyph.LoadFromFile( 'C:\MinhaImagem' + IntToStr(x) + '.bmp' );
  end;

  x := Length(ArrayEdit) - 1;
  Self.Width := LEFT_INICIO + ArrayEdit[x].Left + LARGURA );
  if  x > BOTAO_POR_COULUNA then
      x := BOTAO_POR_COULUNA
  ;
  Self.Height := TOP_INICIO + ArrayEdit[x].Top  + ALTURA;
end;


Espero ter colaborado.
Responder

Gostei + 0

19/11/2010

Frederico Brigatte***

Tente assim
procedure TForm1.Button1Click(Sender: TObject);
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x: integer;
  Botao: TButton;
begin
  for x := 0 to 29 do
  begin
    ArrayEdit[x] := TBitBtn.Create( Self );
    ArrayEdit[x].Parent := Self;
    ArrayEdit[x].Caption := 'Mesa ' + IntToStr( x );
    ArrayEdit[x].OnMouseMove := ControlMouseMove;
    ArrayEdit[x].OnMouseDown := ControlMouseDown;
    ArrayEdit[x].OnMouseUp   := ControlMouseUp;
    ArrayEdit[x].Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    ArrayEdit[x].Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );

    ArrayEdit[x].Glyph.LoadFromFile( 'C:\MinhaImagem' + IntToStr(x) + '.bmp' );
  end;

  x := Length(ArrayEdit) - 1;
  Self.Width := LEFT_INICIO + ArrayEdit[x].Left + LARGURA );
  if  x > BOTAO_POR_COULUNA then
      x := BOTAO_POR_COULUNA
  ;
  Self.Height := TOP_INICIO + ArrayEdit[x].Top  + ALTURA;
end;


Espero ter colaborado.


Para esse exemplo como ficaria:

Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x: integer;
  Botao: TButton;
begin
  for x := 0 to 29 do
  begin
    Botao := TButton.Create( Self );
    Botao.Parent := Self;
    Botao.Caption := 'Botão ' + IntToStr( x + 1 );

    Botao.Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    Botao.Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );
  end;
end;


Responder

Gostei + 0

19/11/2010

Wilson Junior

Tente assim
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x, Ate: integer;
  Botao: TButton;
begin
  Ate := 29;
  for x := 0 to Ate do
  begin
    Botao := TButton.Create( Self );
    Botao.Parent := Self;
    Botao.Caption := 'Botão ' + IntToStr( x + 1 );

    Botao.Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    Botao.Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );
  end;


  Self.Width := LEFT_INICIO + Botao.Left + LARGURA;
  if  Ate > BOTAO_POR_COULUNA then
      Ate := BOTAO_POR_COULUNA
  ;
  Self.Height := TOP_INICIO + ( (Ate mod BOTAO_POR_COULUNA) * ALTURA ) + ALTURA;
end;


Espero ter colaborado.
Responder

Gostei + 0

19/11/2010

Frederico Brigatte***

Tente assim
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x, Ate: integer;
  Botao: TButton;
begin
  Ate := 29;
  for x := 0 to Ate do
  begin
    Botao := TButton.Create( Self );
    Botao.Parent := Self;
    Botao.Caption := 'Botão ' + IntToStr( x + 1 );

    Botao.Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    Botao.Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );
  end;


  Self.Width := LEFT_INICIO + Botao.Left + LARGURA;
  if  Ate > BOTAO_POR_COULUNA then
      Ate := BOTAO_POR_COULUNA
  ;
  Self.Height := TOP_INICIO + ( (Ate mod BOTAO_POR_COULUNA) * ALTURA ) + ALTURA;
end;


Espero ter colaborado.


Deu erro, ficou assim ao executar:


Responder

Gostei + 0

22/11/2010

Frederico Brigatte***

Tente assim
Const
  LEFT_INICIO = 10;
  TOP_INICIO  = 1;
  LARGURA = 90;
  ALTURA  = 30;
  BOTAO_POR_COULUNA = 10;
var
  x, Ate: integer;
  Botao: TButton;
begin
  Ate := 29;
  for x := 0 to Ate do
  begin
    Botao := TButton.Create( Self );
    Botao.Parent := Self;
    Botao.Caption := 'Botão ' + IntToStr( x + 1 );

    Botao.Left := LEFT_INICIO + ( (x div BOTAO_POR_COULUNA) * LARGURA );
    Botao.Top  := TOP_INICIO + ( (x mod BOTAO_POR_COULUNA) * ALTURA );
  end;


  Self.Width := LEFT_INICIO + Botao.Left + LARGURA;
  if  Ate > BOTAO_POR_COULUNA then
      Ate := BOTAO_POR_COULUNA
  ;
  Self.Height := TOP_INICIO + ( (Ate mod BOTAO_POR_COULUNA) * ALTURA ) + ALTURA;
end;


Espero ter colaborado.


Deu erro, ficou assim ao executar:




Nenhuma resposta?
Responder

Gostei + 0

22/11/2010

Marcos Iwazaki

Amigo... o unico problema ae é o seu calculo p achar a altura do form...
se vc testar vai ver q o form fica com pouca altura...
Responder

Gostei + 0

22/11/2010

Wilson Junior

Eu não cheguei a testar, mas altere a linha
Self.Height := TOP_INICIO + ( (Ate mod BOTAO_POR_COULUNA) * ALTURA ) + ALTURA + 40;


Espero ter colaborado.
Responder

Gostei + 0

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar