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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In a recent question ( here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question ( here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

752 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