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 TEditto right-align text. The reason is simple: In order to have right-aligned text in Windows, you have to support multiplelines! Well, an Edit control with multiple lines is really a TMemo. So youcan use a TMemo to simulate a single line TEdit, without the hassle ofbuilding your own component.

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

  1. You need to set WantReturns and WordWrap properties toFalse to ensure that you don’t have text wrapping and that lines don’t disappear when the user presses Enter.
  2. Limit the number of characters that can be enteredinto the TMemo with the MaxLength property.
  3. To prevent the user from inadvertently making text go away with the downarrow (assuming you’ve added some text to the TMemo), in the OnCreate of theform, set the SelStart property to the Length of the TMemo’s text:
    Memo1.SelStart := Length(Memo1.Text);
  4. Trap for key-presses to prevent the user from usingCtrl-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);begin  if Key = #10 then //Ctrl-Enter?    Key := #0; //Kill itend;
To trap for Ctrl-Tab, do the following in the OnKeyDown:
procedure TForm1.Memo1KeyDown(Sender : TObject; var Key : Word; Shift :TShiftState);begin  if (Key = VK_TAB) AND (ssCtrl in Shift) then    Key := 0;end;
The reason for using both OnKeyPress and OnKeyDown is that in the firstinstance, 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 thancreating a new TEdit that will support right-justification of text. In thecase of the TEdit descendant, you’d have to override the Paint method andadd an alignment property that calls the Paint method when its value ischanged. Sounds simple enough, but component writing is never trivial.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

homes in the real estate industry

Exploring the Latest Tech Trends Impacting the Real Estate Industry

The real estate industry is changing thanks to the newest technological advancements. These new developments — from blockchain and AI to virtual reality and 3D printing — are poised to change how we buy and sell homes. Real estate brokers, buyers, sellers, wholesale real estate professionals, fix and flippers, and beyond may

man on floor with data

DevX Quick Guide to Data Ingestion

One of the biggest trends of the 21st century is the massive surge in internet usage. With major innovations such as smart technology, social media, and online shopping sites, the internet has become an essential part of everyday life for a large portion of the population. Due to this internet

payment via phone

7 Ways Technology Has Changed Traditional Payments

In today’s digital world, technology has changed how we make payments. From contactless cards to mobile wallets, it’s now easier to pay for goods and services without carrying cash or using a checkbook. This article will look at seven of the most significant ways technology has transformed traditional payment methods.