[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1515
  • Last Modified:

Compute the length of a Tedit text component

I have a TEdit component on a form
Depending on the length of the string that is in its Text properties, I want to adapt the Tedit width
How can I determine that length ?
Thanks
0
LeTay
Asked:
LeTay
  • 7
  • 6
  • 3
1 Solution
 
btframeworkCommented:
Use functions GetTextExtentPoint32, GetDC
0
 
Geert GruwezOracle dbaCommented:
there is a function in the canvas that does this:

Edit.Canvas.TextWidth(Edit.Text);

you may experience race conditions
The width of the Edit doesn't normally change, and when it does it will force a redraw
determining the width of the text is done in the drawing of the component too
0
 
btframeworkCommented:
Hm. Delphi 6, TEdit has no Canvas property. Or I miss something?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
Geert GruwezOracle dbaCommented:
it is a windowed control and window controls have a Device Context
you just need to wrap it with a little Delphi code to use TCanvas

function EditTextWidth(Edit: TEdit): Integer;
var
  C: TCanvas;
  h: THandle;
begin
  c := TCanvas.Create;
  try
    h := Edit.Handle;
    c.Handle := Edit.GetDeviceContext(h);
    Result := c.TextWidth(Edit.Text);
  finally
    FreeAndNil(c);
  end;
end;
0
 
btframeworkCommented:
Much shorter code you will have if use GetDC and GetTextExtentPoint32 :) (Which do exactly the same as your code).
0
 
LeTayAuthor Commented:
Well, in fact I made a little mistake in my question
I do not really want to adapt the component width
I want to be able to calculate it is too short and take necessary action on the text string
0
 
Geert GruwezOracle dbaCommented:
and what action would that be ?
reduce it ?
0
 
LeTayAuthor Commented:
Yes, reduce the string itself, as it is currently formatted a specific way
0
 
Geert GruwezOracle dbaCommented:
what about a function to give you the portion which is visible ?
function VisibleEditText(Edit: TEdit): string;
var
  C: TCanvas;
  h: THandle;
  x: integer;
begin
  Result := Edit.Text;
  c := TCanvas.Create;
  try
    h := Edit.Handle;
    c.Handle := Edit.GetDeviceContext(h);
    x := c.TextWidth(Result);
    while (x > Edit.Width) and (Result <> '') do
    begin
      Delete(Result, Length(Result), 1);
      x := c.TextWidth(Result);
    end;
  finally
    FreeAndNil(c);
  end;
end;

Open in new window

0
 
LeTayAuthor Commented:
In fact I made a little mistake in my description of the problem
The component used for showing the string is a TLabel, not a TEdit
Is the code from Geert still valid ?
0
 
Geert GruwezOracle dbaCommented:
Ah, that's only a slight difference,
did you not set the Label.AutoSize to true ?

this automatically adjusts the Label size to fit all text of the caption ...

0
 
LeTayAuthor Commented:
In fact, the size and lay-out of the form is such that I have a limited place for this label (there are more of a dozen of such labels)
Autosizing the label is not a solution as it will "hit" another visual component
I need to use fixed width for the label, but I can "adjust" the string if too long
0
 
Geert GruwezOracle dbaCommented:
are all your labels the same font and fontsize and style ?
0
 
LeTayAuthor Commented:
Yes
0
 
Geert GruwezOracle dbaCommented:
actually with a label it's even easier, you've got direct access to the canvas :

it set 2 wide TLabels and some panels

it reduces the caption of the labels to a specified width (in this case width of panel1)

function LimitedText(aText: string; aLabel: TLabel; aMaxWidth: integer; Enforce: boolean = false): string;
var
  x: integer;
begin
  Result := aText;
  x := aLabel.Canvas.TextWidth(Result);
  while (x > aMaxWidth) and (Result <> '') do
  begin
    Delete(Result, Length(Result), 1);
    x := aLabel.Canvas.TextWidth(Result);
  end;
  if Enforce then
    aLabel.Caption := Result;
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var I: Integer;
  C: TControl;
begin
  for I := 0 to Panel2.ControlCount-1 do
  begin
    C := Panel2.Controls[I];
    if C is TLabel then
      LimitedText(TLabel(C).Caption, TLabel(C), Panel1.Width, True);
  end;
end;

Open in new window

0
 
LeTayAuthor Commented:
Thanks a lot !
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 7
  • 6
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now