How to know which object is focused at runtime

5

I saw that you already have a similar question with mine only that is in C # .

I'm doing a field check routine and when one of the date fields is blank it gives the message. I need when one of these fields is in focus, show the field name in the message body.

Code:

if (cbbTipoConsultaEmAberto.ItemIndex   = 5) and
   ((dteDataInicial.Text = '  /  /    ') or
    (dteDataFinal.Text    = '  /  /    ')) then
begin
  if (dteDataInicial.Text = '  /  /    ') then dteDataInicial.SetFocus else
  if (dteDataFinal.Text   = '  /  /    ') then dteDataFinal.SetFocus;
  Application.MessageBox(PChar('Combinação de Seleção Inválida!'+#13+'Campo: 
  '+TDBDateEdit({aqui tenho que saber qual o objeto com o foco}).Hint+
  ' está em branco.'),'Aviso',MB_OK+MB_ICONEXCLAMATION);
  exit;
end;  
    
asked by anonymous 06.04.2018 / 21:18

1 answer

5

In the form you can return the field that is active with the property ActiveControl , in your case it would look something like this:

if (cbbTipoConsultaEmAberto.ItemIndex   = 5) and
   ((dteDataInicial.Text = '  /  /    ') or
    (dteDataFinal.Text    = '  /  /    ')) then
begin
  if (dteDataInicial.Text = '  /  /    ') then dteDataInicial.SetFocus else
  if (dteDataFinal.Text   = '  /  /    ') then dteDataFinal.SetFocus;
  Application.MessageBox(PChar('Combinação de Seleção Inválida!'+#13+'Campo: 
  '+TDBDateEdit(Self.ActiveControl).Hint+
  ' está em branco.'),'Aviso',MB_OK+MB_ICONEXCLAMATION);
  exit;
end;
    
06.04.2018 / 21:32