Hint Inteligente

30/07/2008

0

Olá amigos.
Gostaria de criar uma rotina de ajuste de hints automáticamente para campos derivados de TControl ( ou o mais correto não sei ).
A ídeia é a seguinte: Em tempo de projeto normalmente ajustamos os Fields no Fields Editor , Default Expression, Display Format e outros.
Pensei em usar o Display Label como Hint do campo , já que poderíamos aproveitar os valores escritos (texto) tanto para criação de grids, labels etc.. quanto para hint, evitando assim que tivessemos que repetir ou escrevê-lo nos campos derivados do field em forms diminuindo assim o tamanho do executável e proporcionando ao usuário ao passar o mouse sobre o campo um hint explicativo do campo e sem sobrecarga.
Acho que deveria ao criar o form , atribuir ao hint do campo o hint de cada field presente nos datasets buscando a ligação entre eles campo->dataset->field e completando assim o processo.
Tenho um código genérico no create dos forms que ajusta a cor de todos os componentes de acordo com uma configuração no BD, segue o código...

procedure TFrmFormPadrao.ControleCor(Sender: TObject);
var
i: integer;
begin
for i := 0 to Self.ComponentCount - 1 do
if Self.Components[i] is TControl then
if ((Self.Components[i] is TCustomEdit) or (Self.Components[i] is
TCustomComboBox)) then
if TCustomControl(Self.Components[i]).Tag = 0 then
begin
TCustomControl(Self.Components[i]).OnEnter := ControleEnter;
TCustomControl(Self.Components[i]).OnExit := ControleExit;
end;
end;

Gostaria de aproveitar este código que já varre o form para atribuir os hints mas não estou sabendo de qual classe derivar , se TControl ou TCustonControl etc...
Quem puder dar uma sugestão, ganharemos muito tempo atribuindo hints desta forma automatizada.
Agradeço antecipadamente.

Ps : os Códigos das funções ControleEnter e ControleExit seguem abaixo :

procedure TFrmFormPadrao.ControleEnter(Sender: TObject);
begin
if Assigned(Sender) and (Sender is TControl) then
begin
Cor_Antiga := THackControl(Sender).Color;
THackControl(Sender).Color :=
StringToColor(DtmConexao.CDSConfiguracaoPontoCOR_CAMPO_FOCADO.Value);
end;
end;

procedure TFrmFormPadrao.ControleExit(Sender: TObject);
begin
if Assigned(Sender) and (Sender is TControl) then
THackControl(Sender).Color := Cor_Antiga;
end;

Atençao !!! Declarar em Type

type
THackControl = class(TControl);
THackWinControl = class(TWinControl);
TCustomControl = class(TCustomEdit);

Um abração.


Emerson Azevedo

Emerson Azevedo

Responder

Posts

30/07/2008

Emerson Azevedo

Bom , usando a Getprop (RTTI) ja consigo saber se tem a propriedade Datasource e Datafield.
Segue novo código

procedure TFrmFormPadrao.ControleCor(Sender: TObject);
function TemProp(Comp: TComponent; Prop: string): Boolean;
begin
Result := GetPropInfo(Comp.ClassInfo, Prop) <> nil;
end;
var
I: integer;
PropInfo: PPropInfo;
begin
for I := 0 to Self.ComponentCount - 1 do
if Self.Components[I] is TControl then
if ((Self.Components[I] is TCustomEdit) or (Self.Components[I] is
TCustomComboBox)) then
if TCustomControl(Self.Components[I]).Tag = 0 then
begin
TCustomControl(Self.Components[I]).OnEnter := ControleEnter;
TCustomControl(Self.Components[I]).OnExit := ControleExit;
if TemProp(Self.Components[I], ´DataSource´) then
begin

{ Vai executar o ajuste do Hint aqui }

end;
end;
end;


Estou pensando em testar separado as classes ( porque os combobox são de outra classe ) e tentar uma GetpropValue assim...

if (Self.Components[I] is TCustomEdit) then
(((GetPropValue(Self.Components[I],´Datasource´) as TDataSource).Dataset as TClientDataset).FieldByName....etc

Mas a GetPropValue não aceita conversão de tipos ou estou enganado ?


Responder

30/07/2008

Emerson Azevedo

Estou conseguindo pegar o Nome do Datafield assim:

procedure TFrmFormPadrao.ControleCor(Sender: TObject);
function TemProp(Comp: TComponent; Prop: string): Boolean;
begin
Result := GetPropInfo(Comp.ClassInfo, Prop) <> nil;
end;
var
I: integer;
PropInfo: PPropInfo;
V: Variant;
begin
for I := 0 to Self.ComponentCount - 1 do
if Self.Components[I] is TControl then
if ((Self.Components[I] is TCustomEdit) or (Self.Components[I] is
TCustomComboBox)) then
if TCustomControl(Self.Components[I]).Tag = 0 then
begin
TCustomControl(Self.Components[I]).OnEnter := ControleEnter;
TCustomControl(Self.Components[I]).OnExit := ControleExit;
if TemProp(Self.Components[I], ´DataSource´) then
begin
if (Self.Components[I] is TCustomEdit) then
begin
{Pegando o Nome do Campo }
V := GetStrProp(Self.Components[I],´DataField´);
ShowMessage(V);
{////}
end;
end;
end;
end;

Alguma sugestão para pegar o nome do Datasource ?


Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

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

Aceitar