Alignment in Edit fields

How do I right allign numeric data in a Tedit field?
Who is Participating?
ZifNabConnect With a Mentor Commented:
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.

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

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

/// John
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

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

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

/// John
Simplest way:

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

Let me know if you need code.

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.

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.

mmm, yes strange this happen here, where is Matvey's comment.
kjteng, write, if it is for simple extension, new component is better.
mvsatisAuthor Commented:
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.