Fórum Componentes em tempo de execução. #390838
19/11/2010
0
Frederico Brigatte***
Curtir tópico
+ 0Posts
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]
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
Gostei + 0
19/11/2010
Wilson Junior
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.
Gostei + 0
19/11/2010
Frederico Brigatte***
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;
Gostei + 0
19/11/2010
Frederico Brigatte***
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?
Gostei + 0
19/11/2010
Wilson Junior
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.
Gostei + 0
19/11/2010
Frederico Brigatte***
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?
Gostei + 0
19/11/2010
Frederico Brigatte***
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á.
Gostei + 0
19/11/2010
Wilson Junior
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.
Gostei + 0
19/11/2010
Frederico Brigatte***
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;
Gostei + 0
19/11/2010
Wilson Junior
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.
Gostei + 0
19/11/2010
Frederico Brigatte***
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:
Gostei + 0
22/11/2010
Frederico Brigatte***
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?
Gostei + 0
22/11/2010
Marcos Iwazaki
se vc testar vai ver q o form fica com pouca altura...
Gostei + 0
22/11/2010
Wilson Junior
Self.Height := TOP_INICIO + ( (Ate mod BOTAO_POR_COULUNA) * ALTURA ) + ALTURA + 40;
Espero ter colaborado.
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)
Inserção de url
Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.