Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-09

How many characters fit in a TEdit?

2 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Bart

Hi,

Let me explain the subject of this topic.

Is it possible to determine how many characters fit in a TEdit (at runtime), given that the choosen font is of fixed pitch (like Courier).
Example:
TEdit: Width = 300
Font; Courier New, Size=16

TEdit.GetTextWidth() does not help here, it returns 16 for any font and size (because we cannot access the canvas).

Why do I want to know this?

I have a TEdit.
It is readonly, and it cannot be reached by the cursor (intentionally). When I have text in the control that is wider than the width of the TEdit, the user can click on an arrow (or any other control that I choose for that), and I then want to "scroll" the text in the control.

Say Text = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
Only the 'ABCDEFG' can be displayed in the TEdit.
I can "scroll" by setting SelStart, but to be able how many characters I need to "scroll" each time, I need to know how many characters fit into the TEdit in the first place (7 in this case).

I can sort of guess this at designtime, but at runtime fontsizes may not be the same as on my machine...

Maybe I'm tackling the problem in the wrong way.
What I need is a control that can display text, it should not be "editable" (you cannot put a cursor or caret in it (or at least it should not show it)), the text should be right aligned, and if the text is wider than the control, the text should be srollable by some mechanisme.

Bart

Reply by email

Hans-Peter Diettrich
to Bart
Show quoted text 9 lines
Is it possible to determine how many characters fit in a TEdit (at runtime), given that the choosen font is of fixed pitch (like Courier). Example: TEdit: Width = 300 Font; Courier New, Size=16 TEdit.GetTextWidth() does not help here, it returns 16 for any font and size (because we cannot access the canvas).

You can use it in your edit.OnPaint handler, where you also can scroll the text. Eventually use an TLabel instead of TEdit, with better display control.

DoDi

Reply by email