Cannot focus a disabled or invisible window

Delphi

04/12/2016

Pessoal, o meu problema é muito básico, eu creio, mas não estou conseguindo resolver, já tentei várias formas e não consigo. Vejam só:
1- Tenho um form com um PageControl com várias TABSHEETS;
2. Cada TabSheet possui vários EDITS que preenchem dados para o banco;
3. Quando faço minha validação, se o edit estiver em um TABSHEET não ativo, retorna este erro "Cannot focus a disabled or invisible window", o que é muito óbvio.
4. Na verdade meu código está tentando setar o foco em um edit dentro de outro TABSHEET que não possui o ACTIVECONTROL.

O problema eu entendi perfeitamente, mas não estou acertando o código para efetuar o seguinte algoritimo:
ALGORITIMO QUE NECESSITO DEVE SER MAIS OU MENOS ASSIM:

....minha validacao busca todos os edits requeridos (tag=1) e seta o foco, pinta de amarelo enquanto o foco estiver nele.
se o edit econtrado nao for possivel setar o foco (not canfocus) então deve buscar o controle pai (parent) que no caso seria o tabsheet e depois deve ativar-lo e depois setar o foco do edit respectivo.

Não sei se entenderam, mas preciso muito resolver isso, por favor alguem me ajude.
se alguem tiver uma solução RTTI tambem fico ainda mais agradecido. mas não me refiro aos conceitos de ATTRIBUTES na classe ENTITY. Isso eu já faço, mas quero verificar é os edits(objetos) no form e verificar se estão preenchidos e depois setar o foco neles.

meu atual codigo de verificação é
function FDadosOk : Boolean;
var
i, p : Integer;
begin
{OBS: TAG 1 = campo obrigatorio}
Result := False;
with Screen.ActiveForm do
begin
for i := 0 to ComponentCount -1 do
begin
if Components[i] is TCustomEdit then
if TCustomEdit(Components[i]).Tag = 1 then
if Trim(TCustomEdit(Components[i]).text) = '' then
begin
ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TCustomEdit(Components[i]).name +'" es obligatorio.');
TCustomEdit(Components[i]).SetFocus;
if TCustomEdit(Components[i]).Focused then
TEdit(Components[i]).Color:= -16777192
else TEdit(Components[i]).Color:= -16777211; //cor clInfoBk;
Abort
end;
if Components[i] is TDBLookupComboBox then
if TDBLookupComboBox(Components[i]).Tag = 1 then
if TDBLookupComboBox(Components[i]).Text = '' then
begin
ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TDBLookupComboBox(Components[i]).Name +'" es obligatorio.');
TDBLookupComboBox (Components[i]).SetFocus;
if TDBLookupComboBox(Components[i]).Focused then TDBLookupComboBox(Components[i]).Color:= -16777192 else TEdit(Components[i]).Color:= -16777211; //cor clInfoBk;
Abort
end;
if Components[i] is TComboBox then
if TComboBox(Components[i]).Tag = 1 then
begin
if TComboBox(Components[i]).Style in [csDropDownList, csOwnerDrawFixed, csOwnerDrawVariable] then
if TComboBox(Components[i]).ItemIndex = -1 then
begin
ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TComboBox(Components[i]).Name +'" es obligatorio.');
TComboBox (Components[i]).SetFocus;
if TComboBox(Components[i]).Focused then TComboBox(Components[i]).Color:= -16777192 else TEdit(Components[i]).Color:= -16777211; //cor clInfoBk;
Abort
end;

if TComboBox(Components[i]).Style in [csDropDown, csSimple] then
if TComboBox(Components[i]).Text = '' then
begin
ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TComboBox(Components[i]).Name +'" es obligatorio.');
TComboBox (Components[i]).SetFocus;
if TComboBox(Components[i]).Focused then TComboBox(Components[i]).Color:= -16777192 else TEdit(Components[i]).Color:= -16777211; //cor clInfoBk;
Abort
end;
end;
end;
result:= True;
end;
end;


Se souberem alguma outra função generica ainda melhor, fico agradecido.

Obrigado amigos.
Wilton Santos

Wilton Santos

Curtidas 0

Melhor post

Dorivan Sousa

Dorivan Sousa

05/12/2016

if TDBLookupComboBox (Components[i]).Parent is TTabSheet then
begin
TTabSheet(TDBLookupComboBox (Components[i]).Parent).Show;
end;

TDBLookupComboBox (Components[i]).SetFocus;
GOSTEI 2

Mais Respostas

Wilton Santos

Wilton Santos

04/12/2016

Meu caro amigo Dorivan, que simples e eficiente. Muito obrigado, deu certinho.
GOSTEI 0
Wilton Santos

Wilton Santos

04/12/2016

Detalhe,
Só funciona se o componente PAI (parent) for um tabsheet diretamente, mas se o TABSHEET pai possuir um edit que tiver dentro de um PANEL ou de um GROUPBOX ou dos dois juntos, isto eh, dentro de um GROUPBOX que está dentro de um PANEL que está dentro de um TABSHEET, então tenho que sair dando SHOW em todo mundo de um por um? E se eu não souber dentro de que componente etá determinado controle, por exemplo.. .Vai que em certo tabsheet tenho um edit dentro de um panel, no outro tenho dentro de um groupbox e no terceiro tabsheet, tenho um edit dentro dos dois e ainda em um quarto, tenho um componente diretamente em um tabsheet cru. Como tornar isso generico sem muita repeticao de ifs? ou como simplesmente resolver isso?
GOSTEI 0
Wilton Santos

Wilton Santos

04/12/2016

BOA TARDE,
FIZ ASSIM, ele seleciona o TABSHEET e foca o edit de forma perfeita, mas quando preencho o edit, gera este erro: 'ARGUMENT OUT OF RANGE' e nao sai do controle.

segue o codigo:
function FDadosOk : Boolean;
var
  i : Integer;
begin
{OBS: TAG 1 = campo obrigatorio}
Result := False;
  with Screen.ActiveForm do
   begin
    for i := 0 to ComponentCount -1 do

  //CUSTOMEDIT
      if Components[i] is TCustomEdit then  //verifica se eh um edit
       if TCustomEdit(Components[i]).Tag = 1 then  //verifica se eh requerido
        if Trim(TCustomEdit(Components[i]).text) = '' then  //verifica se esta vazio
     if TCustomEdit(Components[i]).Parent is TGroupBox then  //verifica se o controle pai eh um groupbox
       Begin
          if TGroupBox(TCustomEdit(Components[i]).Parent).Parent is TTabSheet then //verifica se o controle pai do groupbox eh um Tabsheet
              begin
              ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TCustomEdit(Components[i]).name +'" es obligatorio.');
              TTabSheet(TGroupBox(TCustomEdit(Components[i]).Parent).Parent).Show; //Mostre o tabsheet
              TCustomEdit(Components[i]).SetFocus; //foca o controle
              if TCustomEdit(Components[i]).Focused then //se o controle tiver focado
                  TEdit(Components[i]).Color:= -16777192;
                  if (not TCustomEdit(Components[i]).Focused) and (Trim(TCustomEdit(Components[i]).text) = '') then
                   TEdit(Components[i]).Color:= -16777211;  //cor clInfoBk;
              Abort
             end
          else
            if TGroupBox(TCustomEdit(Components[i]).Parent).Parent is TPanel then
            if TPanel(TGroupBox(TCustomEdit(Components[i]).Parent).Parent).Parent is TTabSheet then
          begin
          ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TCustomEdit(Components[i]).name +'" es obligatorio.');
          TTabSheet(TPanel(TGroupBox(TCustomEdit(Components[i]).Parent).Parent).Parent).Show;
          TCustomEdit(Components[i]).SetFocus;
          if TCustomEdit(Components[i]).Focused then //se o controle tiver focado
                  TEdit(Components[i]).Color:= -16777192;
                  if (not TCustomEdit(Components[i]).Focused) and (Trim(TCustomEdit(Components[i]).text) = '') then
                   TEdit(Components[i]).Color:= -16777211;  //cor clInfoBk;
          Abort
         end;
        End
     else
     begin
      if TCustomEdit(Components[i]).Parent is TPanel then
        begin
         if TPanel(TCustomEdit(Components[i]).Parent).parent is TTabSheet then
         TTabSheet(TPanel(TCustomEdit(Components[i]).Parent).parent).Show;
         TCustomEdit(Components[i]).SetFocus;
       if TCustomEdit(Components[i]).Focused then //se o controle tiver focado
            TEdit(Components[i]).Color:= -16777192;
              if (not TCustomEdit(Components[i]).Focused) and (Trim(TCustomEdit(Components[i]).text) = '') then
               TEdit(Components[i]).Color:= -16777211;  //cor clInfoBk;
        Abort
        end
      else if TCustomEdit(Components[i]).Parent is TTabSheet then
         begin
           if TPanel(TCustomEdit(Components[i]).Parent).parent is TTabSheet then
           TTabSheet(TPanel(TCustomEdit(Components[i]).Parent).parent).Show;
           TCustomEdit(Components[i]).SetFocus;
          if TCustomEdit(Components[i]).Focused then
              TEdit(Components[i]).Color:= -16777192
          else TEdit(Components[i]).Color:= -16777211;  //cor clInfoBk;
          Abort
          end;
        end;

 //DBLOOKUP
      if Components[i] is TDBLookupComboBox then
       if TDBLookupComboBox(Components[i]).Tag = 1 then
        if TDBLookupComboBox(Components[i]).Text = '' then
    if TDBLookupComboBox(Components[i]).Parent is TTabSheet then
      begin
      ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TDBLookupComboBox(Components[i]).Name +'" es obligatorio.');
      TTabSheet(TDBLookupComboBox(Components[i]).Parent).Show;
      TDBLookupComboBox (Components[i]).SetFocus;
      if TDBLookupComboBox(Components[i]).Focused then TDBLookupComboBox(Components[i]).Color:= -16777192 else TEdit(Components[i]).Color:= -16777211;  //cor clInfoBk;
      Abort
      end
    else
    if TDBLookupComboBox(Components[i]).Parent is TGroupBox then
       Begin
        if TGroupBox(TDBLookupComboBox(Components[i]).Parent).Parent is TTabSheet then
          begin
          ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TDBLookupComboBox(Components[i]).Name +'" es obligatorio.');
          TTabSheet(TGroupBox(TDBLookupComboBox(Components[i]).Parent).Parent).Show;
          TDBLookupComboBox (Components[i]).SetFocus;
          if TDBLookupComboBox(Components[i]).Focused then TDBLookupComboBox(Components[i]).Color:= -16777192 else TEdit(Components[i]).Color:= -16777211;  //cor clInfoBk;
          Abort
          end
        else
          if TGroupBox(TDBLookupComboBox(Components[i]).Parent).Parent is TPanel then
           if TPanel(TGroupBox(TDBLookupComboBox(Components[i]).Parent).Parent).Parent is TTabSheet then
            begin
            ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TDBLookupComboBox(Components[i]).Name +'" es obligatorio.');
            TTabSheet(TPanel(TGroupBox(TDBLookupComboBox(Components[i]).Parent).Parent).Parent).Show;
            TDBLookupComboBox (Components[i]).SetFocus;
            if TDBLookupComboBox(Components[i]).Focused then TDBLookupComboBox(Components[i]).Color:= -16777192 else TEdit(Components[i]).Color:= -16777211;  //cor clInfoBk;
            Abort
            end;
       end
    else if TDBLookupComboBox(Components[i]).Parent is TPanel then
         begin
         if TPanel(TDBLookupComboBox(Components[i]).Parent).Parent is TTabSheet then
              begin
              ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TDBLookupComboBox(Components[i]).Name +'" es obligatorio.');
              TTabSheet(TPanel(TDBLookupComboBox(Components[i]).Parent).parent).Show;
              TDBLookupComboBox (Components[i]).SetFocus;
              if TDBLookupComboBox(Components[i]).Focused then TDBLookupComboBox(Components[i]).Color:= -16777192 else TEdit(Components[i]).Color:= -16777211;  //cor clInfoBk;
              Abort
              end
         end;

  //COMBOBOX STYLE 1
    if Components[i] is TComboBox then
     if  TComboBox(Components[i]).Tag = 1 then
       begin
          if TComboBox(Components[i]).Style in [csDropDownList, csOwnerDrawFixed, csOwnerDrawVariable] then
            if TComboBox(Components[i]).ItemIndex = -1 then
            if TComboBox(Components[i]).Parent is TTabSheet then
               begin
                ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TComboBox(Components[i]).Name +'" es obligatorio.');
                TTabSheet(TComboBox(Components[i]).Parent).Show;
                TComboBox (Components[i]).SetFocus;
                if TComboBox(Components[i]).Focused then TComboBox(Components[i]).Color:= -16777192 else TEdit(Components[i]).Color:= -16777211;  //cor clInfoBk;
                Abort
               end
            else
            if TComboBox(Components[i]).Parent is TPanel then
              begin
                if TPanel(TComboBox(Components[i]).Parent).Parent is TTabSheet then
                 begin
                  ShowMessage('¡OOPS! '+dm.ZQLoginuser_login.AsString+ ', el campo: "'+ TComboBox(Components[i]).Name +'" es obligatorio.');
                  TTabSheet(TPanel(TComboBox(Components[i]).Parent).Parent).Show;
                  TComboBox (Components[i]).SetFocus;
                  if TComboBox(Components[i]).Focused then TComboBox(Components[i]).Color:= -16777192 else TEdit(Components[i]).Color:= -1
GOSTEI 0
Teixeira4

Teixeira4

04/12/2016

http://stackoverflow.com/questions/15237290/delphi-error-cannot-focus-a-disabled-or-invisible-window
GOSTEI 0
POSTAR