Fórum Utilizando o (Sender as Classe).NomeMetodo. #267325
03/02/2005
0
Tenho um vetor com ClassNames.
Classes[0] := TDBEdit;
Classes[1] := TEdit;
Classes[2] := TComboBox;
Classes[3] := TDBComboBox;
Como eu colocaria por exemplo (Componente as VetClass[x]).Text ? O Delphi da erro pois não reconhece o TEXT.
Eu queria fazer o seguinte
IF (Componente is VetClass[x]) Then
(Componente as VetClass[x]) .Text := ´xx´;
Lembrando que todas classes do meu vetor tem o Metodo .Text.
Falow
Valeu pessoal
Yalle Cunha.
Yallebr
Curtir tópico
+ 0Posts
03/02/2005
Tathianam
Não sei se é bem isso que vc quer, mas lá vai...
Para usar:
LimaForm(form1);
procedure LimpaForm(xForm: TForm); var I : integer; xName: string; begin with xForm do begin for I := ComponentCount - 1 downto 0 do begin xName := Components[I].Name; try if (Components[i] is TEdit) then TEdit(Components[i]).Clear ... TODAS AS CLASSES QUE VC QUER USAR else if (Components[i] is TMemo) then TMemo(Components[I]).TabStop := false except on E: Exception do Messagedlg(´ERRO LIMPA: ´+xName+´ ´+E.Message,mtError,[mbOk],E.HelpContext); end; end; end; end;
Gostei + 0
03/02/2005
Massuda
type TCrackedControl = class(TControl); // ... // talvez seja redundante testar se é TControl! IF (Componente is TControl) and (Componente is VetClass[x]) Then TCrackedControl(Componente) .Text := ´xx´;
Outra forma seria usar RTTI; veja este tópico sobre como [url=http://forum.clubedelphi.net/viewtopic.php?t=56637]ver se o controle possui determinada propriedade[/url] e use as funções SetXxxProp declaradas na unit TypInfo para ajustar o valor da propriedade.
Gostei + 0
03/02/2005
Yallebr
Valeu pela ajuda. Nenhuma funcionou.
A dica do tathianam está que funciona. Mas eu não quero fazer um monte de IF. Queria verificar isso em um loop nas classes.
O do Massuda naõ funcionou pois não tem um classe padrão a todos componentes (Edit, Combobox) que tem a propriedade Label.
Eu estou tentando utilizar RTTI, mas está dando pau minha aplicação nesse ponto.
propInfo := getPropInfo(c.classinfo,Text);
if assigned(propinfo) then //If found
begin
l := getordprop(c,propInfo); //Aqui eu já vejo se tem a propriedade TExt no componente. Porém com agora eu consigo editar?
dsname := TDataSet(l).Name; //Aqui o que coloco? Não pode ser TDataSet(L).name. Pois não sei qual é o tipo de componente. So sei que tem propriedade Text.
Aff. Ta complicado.
Valeu pessoal.
Yalle Cunha.
Gostei + 0
03/02/2005
Massuda
uses TypInfo, // ... function GetPropCount(const AInstance: TPersistent): Integer; // Determina o total de propriedades publicadas por um objeto var TypeDataP: PTypeData; begin TypeDataP := GetTypeData(AInstance.Classinfo); Result := TypeDataP^.PropCount; end; function GetPropName(const AInstance: TPersistent; const AIndex: Integer): string; // Obtem o nome de uma propriedade publicada por um objeto var PropListP: PPropList; PropInfoP: PPropInfo; TypeDataP: PTypeData; begin Result := ´´; TypeDataP := GetTypeData(AInstance.Classinfo); GetMem(PropListP, TypeDataP^.PropCount * SizeOf(PPropInfo)); try GetPropInfos(AInstance.ClassInfo, PropListP); PropInfoP := PropListP^[AIndex]; Result := PropInfoP^.Name; finally FreeMem(PropListP, TypeDataP^.PropCount * SizeOf(PPropInfo)); end; end; procedure SetControlProperty(const AControl: TControl; const APropertyName, APropertyValue: string); // Atribui um valor a uma propriedade de um controle. var PropertyCount: Integer; PropertyName: string; SubControl: TControl; S: string; N: Integer; begin // Verifica se e uma propriedade "composta", do tipo font.color N := Pos(´.´, APropertyName); if N > 0 then begin // Determina qual o subcontrole... S := Copy(APropertyName, 1, Pred(N)); SubControl := TControl(GetOrdProp(AControl, S)); // ...qual a propriedade... S := Copy(APropertyName, Succ(N), MaxInt); // ...e ajusta o valor SetControlProperty(SubControl, S, APropertyValue); end else begin // Determina o total de propriedades publicadas pelo controle PropertyCount := GetPropCount(AControl); // Busca pela propriedade for N := 0 to Pred(PropertyCount) do begin PropertyName := GetPropName(AControl, N); if CompareText(PropertyName, APropertyName) = 0 then begin // Atribui o valor case PropType(AControl, APropertyName) of tkLString, tkWString, tkString: SetStrProp(AControl, APropertyName, APropertyValue); tkChar, tkInteger: // Valores do tipo TColor não são enumeracoes... :o( if CompareText(Copy(APropertyValue, 1, 2), ´cl´) = 0 then begin try SetOrdProp(AControl, APropertyName, StringToColor(APropertyValue)); except SetOrdProp(AControl, APropertyName, StrToIntDef(APropertyValue, 0)); end; end // Valores do tipo TCursor não são enumeracoes... :o( else if CompareText(Copy(APropertyValue, 1, 2), ´cr´) = 0 then begin try SetOrdProp(AControl, APropertyName, StringToCursor(APropertyValue)); except SetOrdProp(AControl, APropertyName, StrToIntDef(APropertyValue, 0)); end; end else begin SetOrdProp(AControl, APropertyName, StrToInt(APropertyValue)); end; tkEnumeration: SetEnumProp(AControl, APropertyName, APropertyValue); tkInt64: SetInt64Prop(AControl, APropertyName, StrToInt(APropertyValue)); tkFloat: SetFloatProp(AControl, APropertyName, StrToFloat(APropertyValue)); tkSet: SetSetProp(AControl, APropertyName, APropertyValue); tkClass: begin if TPersistent(GetOrdProp(AControl, APropertyName)) is TStrings then begin // Os itens devem estar separados por #1310 na string TStrings(GetOrdProp(AControl, APropertyName)).Text := APropertyValue; end; end; end; // Encontrou a propriedade, pode parar Break; end; end; end; end;
Para usar:
SetControlProperty(Edit1, ´Text´, ´Texto exemplo´); SetControlProperty(Label1, ´Top´, ´40´);
Se você tentar alterar uma propriedade que não existe no componente, nada acontece.
Essa rotina tem alguns furos, mas resolve a maior parte dos casos.
Gostei + 0
03/02/2005
Dopi
procedure TForm1.Button2Click(Sender: TObject); Var I : Integer ; c : TComponent; propInfo: PPropInfo; begin for i:=0 to ComponentCount-1 do begin c := components[i]; propInfo := getPropInfo(c.classinfo,´Text´); if assigned(propinfo) then //If found SetPropValue(c,´Text´,´TESTANDO´); end; end ;
Gostei + 0
03/02/2005
Marcelo Saviski
[quote:a0998ab604=´yallebr, respondendo em outro tópico´]Pessoal agora o código funcionou.
Obrigado novamente, vou estudar o código para fazer algumas modificações.
Yalle Cunha[/quote:a0998ab604]
Gostei + 0
03/02/2005
Yallebr
Sem querer abusar, pois já me ajudaram de mais.
Saberiam me dizer como poderia fazer assim:
SetPropValue(c,´DataSource´,nil); Todas as propriedades eu consegui.
Nesse caso da o seguinte erro.
[Error] Unit1.pas(55): Incompatible types: ´Variant´ and ´Pointer´
Novamente obrigado.
Yalle Cunha.
Gostei + 0
03/02/2005
Dopi
SetPropValue(c,´DataSource´,Null);
Ps: Uses Variants
Gostei + 0
Clique aqui para fazer login e interagir na Comunidade :)