How to know which object is focused at runtime

Asked

Viewed 588 times

5

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

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

Codice:

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;  

1 answer

5


In the form you can return the field that is active with the property ActiveControl, in your case would look more or less 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;
  • 1

    So from 'access Violation'

  • 2

    I managed to.... I removed the . Name and it worked... it was like this ...TDBDateEdit(Self.ActiveControl).Hint...

  • 1

    I applied the correction to the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.