Converter String em Label

Delphi

05/09/2005

Precisso fazer isso

for i := 1 to 200 do
´ME´+IntToStr(i).Caption:= ´Algo´;


Exemplo

ME1.Caption:= ´Algo´;
ME2.Caption:= ´Algo´;
ME3.Caption:= ´Algo´;
ME4.Caption:= ´Algo´;
ME5.Caption:= ´Algo´;
...

Eu tenho 200 Label e precisso que coloque todos iguais então como vou fazer isso??


Rudá

Rudá

Curtidas 0

Respostas

Steve_narancic

Steve_narancic

05/09/2005

Faz algo do tipo:

var
 lb: TLabel;
 i: integer;
begin
 for i:= 0 to 200 do
 begin
  lb := TLabel.Create();
  lb.Name := ´ME´+IntToStr(i);
  lb.Parent:= CRM_Principal;
  lb.Top:= i;
  lb.Left:= i;
  lb.Caption := ´ME´+IntToStr(i);
  lb.Visible:= true;
 end;



GOSTEI 0
Michael

Michael

05/09/2005

Olá colega!

Se vc tem absoluta certeza que todos os labels existem, pode usar o seguinte código:

for I := 1 to 200 do
  TLabel(FindComponent(´ME´ + IntToStr(I))).Caption := ´Algo´;


Se vc não tem esta certeza, então use este outro:

var oControl : TComponent;
(...)
for I := 1 to 200 do
begin
  oControl := FindComponent(´ME´ + IntToStr(I));
  
  if Assigned(oControl) then  
    TLabel(oControl).Caption := ´Algo´;
end;


[]´s


GOSTEI 0
Steve_narancic

Steve_narancic

05/09/2005

Depois eu vi que você ja tinha os labels:

 for i:= 0 to ComponentCount do
 begin
  if Components[i] is TLabel
  then (Components[i] as TLabel).Caption := ´Algo´;
 end;



GOSTEI 0
Massuda

Massuda

05/09/2005

Para localizar um componente em um form pelo nome (propriedade Name), use o método TForm.FindComponent...
var
  Componente: TComponente;
....
  Componente := FindComponent(´NomeDoComponente´);
  if Componente <> nil then begin
    // encontrou o componente
  end;
...



GOSTEI 0
Rudá

Rudá

05/09/2005

Valeu gente era isso mesmo que eu queria.


GOSTEI 0
POSTAR