Como dar foco a TEdit, independente da visibilidade
13/01/2010
0
Bom dia.
Como forçar o foco do TEdit que não esta visível ? por exemplo ele esta dentro de um dos TabSheets que não é o corrente.
if oEdit.canFocus then
oEdit.SetFocus;
... naturamente isso não funcionará em qualquer situação.
Gostaria de elaborar uma forma genérica, usando somente o objeto do componente em si, sem precisar informar os objetos form, etc.
Como os objetos tem a propriedade Parent, que indica em qual classe esta vinculado, acredito que precisaria da foco na ordem inversa (maior ancestral) até chegar no TEdit.
Eric
Curtir tópico
+ 0
Responder
Posts
13/01/2010
Alan Souza
usando o seu exemplo do post anterior:
function CallOnExit( oFrm: TObject ): Boolean ;
var iPos: Integer;
var oCtrl: TObject;
begin result:=true;
for iPos:=1 to TForm(oFrm).ComponentCount-1 do begin
oCtrl:=TForm(oFrm).Components[iPos];
if Assigned(TEdit(oCtrl).OnExit) then begin
TEdit(oCtrl).Tag:=0; //
TEdit(oCtrl).OnExit(TEdit(oCtrl));
if TEdit(oCtrl).Tag < 0 then begin
result:=false;
if not TEdit(oCtrl).CanFocus then
TornaVisivelParaFoco(TEdit(oCtrl));
TEdit(oCtrl).SetFocus; exit;
end;
end;
end;
procedure TornaVisivelParaFoco(comp: TWinControl);
var
win: TWinControl;
begin
win := comp;
while Assigned(win.Parent) do
begin
if win.Parent is TTabSheet then //esse if é um "recurso" para o PageControl mostrar o TabSheet associado à aba correta.
TTabSheet(win.Parent).PageControl.ActivePage := TTabSheet(win.Parent);
win.Visible := True;
win := win.Parent;
end;
end;
testei com um Edit dentro de Panels, GroupBoxes e TabSheets e funcionou normalmente.
function CallOnExit( oFrm: TObject ): Boolean ;
var iPos: Integer;
var oCtrl: TObject;
begin result:=true;
for iPos:=1 to TForm(oFrm).ComponentCount-1 do begin
oCtrl:=TForm(oFrm).Components[iPos];
if Assigned(TEdit(oCtrl).OnExit) then begin
TEdit(oCtrl).Tag:=0; //
TEdit(oCtrl).OnExit(TEdit(oCtrl));
if TEdit(oCtrl).Tag < 0 then begin
result:=false;
if not TEdit(oCtrl).CanFocus then
TornaVisivelParaFoco(TEdit(oCtrl));
TEdit(oCtrl).SetFocus; exit;
end;
end;
end;
procedure TornaVisivelParaFoco(comp: TWinControl);
var
win: TWinControl;
begin
win := comp;
while Assigned(win.Parent) do
begin
if win.Parent is TTabSheet then //esse if é um "recurso" para o PageControl mostrar o TabSheet associado à aba correta.
TTabSheet(win.Parent).PageControl.ActivePage := TTabSheet(win.Parent);
win.Visible := True;
win := win.Parent;
end;
end;
testei com um Edit dentro de Panels, GroupBoxes e TabSheets e funcionou normalmente.
Responder
Clique aqui para fazer login e interagir na Comunidade :)