Alignment in Edit fields

Posted on 1998-08-18
Last Modified: 2010-04-04
How do I right allign numeric data in a Tedit field?
Question by:mvsatis
  • 3
  • 3
  • 2
  • +2

Expert Comment

ID: 1337214
I can give you a component called a TCurrencyEdit it has an Alignment property (right, center, left) it also has decimal places and DisplayFormat so you can put in things like $, lbs, is from the Rx Libraries, you can download them free at


Accepted Solution

ZifNab earned 100 total points
ID: 1337215
Hi mvsatis,

Here is the answer (from delphi Corner) to your question, but not one you like I suppose :


TEdit and right-justification of text
How can I get a TEdit to display right-justified text?

There is no way to do this at runtime because there's no facility in TEdit to right-align text. The reason is simple: In order to have right-aligned text in Windows, you have to support multiple lines! Well, an Edit control with multiple lines is really a TMemo. So you can use a TMemo to simulate a single line TEdit, without the hassle of building your own component.

There are a few things you should be aware of if you do this, though.

You need to set WantReturns and WordWrap properties to False to ensure that you don't have text wrapping and that lines don't disappear when the user presses Enter.
Limit the number of characters that can be entered into the TMemo with the MaxLength property.
To prevent the user from inadvertently making text go away with the down arrow (assuming you've added some text to the TMemo), in the OnCreate of the form, set the SelStart property to the Length of the TMemo's text:
Memo1.SelStart := Length(Memo1.Text);
Trap for key-presses to prevent the user from using Ctrl-Enter and Ctrl-Tab, which are hard returns and hard tabs, respectively. To trap for Ctrl-Enter, write the following in the OnKeyPress event of the TMemo:
procedure TForm1.Memo1KeyPress(Sender : TObject; var Key : Char);
  if Key = #10 then //Ctrl-Enter?
    Key := #0; //Kill it
To trap for Ctrl-Tab, do the following in the OnKeyDown:

procedure TForm1.Memo1KeyDown(Sender : TObject; var Key : Word; Shift :
  if (Key = VK_TAB) AND (ssCtrl in Shift) then
    Key := 0;
The reason for using both OnKeyPress and OnKeyDown is that in the first instance, I didn't need to trap a Shift state, but in the second I did need to.

This seems like a lot, but actually it's a far simpler than creating a new TEdit that will support right-justification of text. In the case of the TEdit descendant, you'd have to override the Paint method and add an alignment property that calls the Paint method when its value is changed. Sounds simple enough, but component writing is never trivial.

Copyright © 1995, 1996, 1997 Brendan V. Delumpa All Rights Reserved


So, using a TEdit won't get you to what you want. So what can you do? 1. Use a TMemo, like above OR 2. make an own component OR 3. Use an already made component! And there are already a lot of them on the web. One I usually use is the one of RLib, because I like this freeware library a lot (see Matvey's comment).

Regards, Zif.

PS. Matvey, hope you don't mind I've answered this one. But I did this, because it's right on to the question.


Expert Comment

ID: 1337216
On DSP there is a fw component that might do it. Haven't tested it myself, so...

/// John

Expert Comment

ID: 1337217
NOW I have tested it, and it seems to work very well...?!?!

/// John

Expert Comment

ID: 1337218
...It is actually a simple solution to some other of my needs...
.especially valid char masking.
Thank you David Robledo Fernández!

/// John
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.


Expert Comment

ID: 1337219
Simplest way:

Subclass the TEdit component
Override the createParam method
In your createParam method, set the ParamStyle

Let me know if you need code.


Expert Comment

ID: 1337220
kjteng, that's indeed a simple way, make another component. But then again, why not using one which already exist?

Ok, but we all agree that it isn't possible to do with the native TEdit... except is is somewhat possible with the native TMemo.


Expert Comment

ID: 1337221
Sorry I didn't see your answer when I post the comment - something wrong with my browser.

Why not using one which already exist?
If ( what we want is just a simple task such as aligning text)
  and (we do not need the extra features provided by the third   party component) then
it is better to write our own component as this will minimise the overheads.

By the way where is Matvey's comment? I can see it in the browser.


Expert Comment

ID: 1337222
mmm, yes strange this happen here, where is Matvey's comment.
kjteng, write, if it is for simple extension, new component is better.

Author Comment

ID: 1337223
OK I have tested your solution and it works,but it is a little bit teddious.
Now I have found on the net several components who will tho this job more easaly.

Look to (excellent site) or DSP.


Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now