Search code examples
delphiuser-interfacedelphi-xe2editcontrol

Is it possible to determine if the text in a dbEdit is longer than what is visible?


On some forms I have dbEdits that sometimes aren't wide enough to show all the text their fields may contain. For them I have the following code:

procedure Tgm12edLots.dbeLotNameMouseEnter(Sender: TObject);
begin
  with dbeLotName do begin
    ShowHint := True;
    Hint := Text;
  end;
end;

I'd like to avoid the hint showing if all the text is visible, but I don't how to test for that condition.

Thanks for any tips/suggestions!


Solution

  • I think this should work...

    function CanShowAllText(Edit: TDBEdit):Boolean;
    var
        TextWidth:Integer;
        VisibleWidth: Integer;
        Bitmap: TBitmap;
    const
    //This could be worked out but without delphi I can't remember all that goes into it.
        BordersAndMarginsWidthEtc = 4;
    begin
        Bitmap := TBitmap.Create;
        try
            Bitmap.Canvas.Font.Assign(Edit.Font);
            TextWidth := Bitmap.Canvas.TextWidth(Edit.Text);
            VisibleWidth := Edit.Width - BordersAndMarginsWidthEtc;
            Result := TextWidth < VisibleWidth;
        finally
            Bitmap.Free;
        end;
    end;