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
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.


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

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 62
Adoquery sql  left join does not work 25 99
Convert MS Word document to a PDF file 9 92
RESTRequest Parameter 4 43
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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