Show hint when component receives focus

Asked

Viewed 1,650 times

1

How do I show the hint of a Tedit when it receives focus, without the mouse being on top of it? I didn’t put code because I don’t have a clue.

2 answers

1


Work the following code at the event OnEnter of) TEdit(s) desired:

Create the necessary variables:

var
    Origin : TPoint; // Receberá a origem do Form na tela
    HintWindow : THintWindow; // Objeto Hint do delphi
    Hint : String; // Receberá a Hint do Edit
    EditL, EditT, EditR, EditB : Integer; // Parâmetros do retângulo da Hint

Set the values of the variables:

Origin := ClientToScreen(Point(0,0)); //Origem do Form na tela
Hint := (Sender as TControl).Hint;  //Atribua a Hint do Edit

Calculate positions for the Hint rectangle:

EditL := Origin.X + (Sender as TControl).Left + 20;
EditT := Origin.Y + (Sender as TControl).Top - 12;
EditR := EditL + Length(Hint) * 9; 
EditB := EditT + 18;

Create a new Hint object; show it in the rectangle calculated on the previous line:

HintWindow := THintWindow.Create(nil);
HintWindow.ActivateHint( Rect(EditL, EditT, EditR, EditB), Hint );

The moment you want to release Hint just use HintWindow.Free.

I suggest you release her at the event OnExit Edit or using a 5 to 8 second Timer for a traditional Windows effect.

  • Do you think it works for a devexpress Tcxdbtextedit? It has the Clienttoscreen property.

  • Make sure he’s descended from TControl and voila.

1

Another way to do this, which also works on top of events, is to create your own suggestion window with the class THintWindow as it suggests this article.

  1. Set the following variables:

    FActive: boolean;
    FHint: THintWindow;
    

    Updating: Below the clause Uses define the constant:

    Uses
    //....
    
    Const
      UM_EXITPROC = WM_USER + 42;
    
  2. Use the method below to disable the suggestion window:

    procedure DesativarSugestao;
    begin
      FActive := false;
      if Assigned(FHint) then
        begin
          FHint.ReleaseHandle;
          FHint.Free;
          FHint := nil;
        end;
    end;
    
  3. Use the method below to receive the right and left mouse click messages:

    procedure AppMessage(var AMessage: TMsg; var Handled: Boolean);
    begin
      if (AMessage.Message = WM_LBUTTONDOWN) or (AMessage.Message = WM_RBUTTONDOWN) then
        if Assigned(FHint) and FHint.Visible then
          DesativarSugestao;
    end;
    
  4. Method responsible for displaying the suggestion box:

    procedure mostrarSugestao(Sender: TObject);
    Var
       P: TPoint;
       R: TRect;
       Sugestao: string;
    begin
      if Assigned(FHint) and FActive then
         Exit;
      P.X := (Sender As TEdit).Left;
      P.Y := (Sender As TEdit).Top - 24;
      with R do
        begin
          topLeft := ClientToScreen(P);
          Right := Left + 150;
          Bottom := Top + 18;
        end;
    
      Sugestao := 'A sugestão é...'; // Ou (Sender As TEdit).Text
      FHint := THintWindow.Create(Self);
      FHint.ActivateHint(R, Sugestao);
      FActive := True;
      PostMessage(Handle, UM_EXITPROC, 0, 0);
    end;
    
  5. At the event OnEnter of Edit do:

    procedure Edit1Enter(Sender: TObject);
    begin
      if not Assigned(FHint) and FActive = False then
        mostrarSugestao(Sender);
    end;
    
  6. At the event OnExit of Edit do:

    procedure Edit1Exit(Sender: TObject);
    begin
      if Assigned(FHint) and FActive then
        DesativarSugestao;
    end;
    
  7. At the event OnKeyPress of Edit do:

    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
    begin
      if Assigned(FHint) and FActive then
        DesativarSugestao;
    end;
    
  8. At the event OnMouseEnter do:

    procedure Edit1MouseEnter(Sender: TObject);
    begin
      if not Assigned(FHint) and FActive = False then
        mostrarSugestao(Sender);
    end;
    
  9. At the event OnMouseLeave do:

    procedure Edit1MouseLeave(Sender: TObject);
    begin
      if Assigned(FHint) and FActive then
        DesativarSugestao;
    end;
    
  10. At the event OnClick of Edit do:

    procedure TForm1.Edit1Click(Sender: TObject);
    begin
      if Assigned(FHint) and FActive then
        DesativarSugestao;
    end;
    
  11. At the event OnCreate of the form do:

    procedure FormCreate(Sender: TObject);
    begin
      Application.OnMessage := AppMessage;
    end;
    

The result should be similar to this:

inserir a descrição da imagem aqui

  • I’ll take a look, maybe it’s even better for me since I use another type of Tedit. Thank you.

  • @Artur_indio I’m happy to help, I edited the answer, I forgot to quote a necessary constant.

Browser other questions tagged

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