Re: Delphi right-aligned TEdit
Posted by webmaster Guido on November 21, 2002
In Reply to: Alignment posted by Barry Myers p12275 on November 16, 2002:
: I want to have a field for numeric entries that will have a numeric mask (that is, it will accept only numeric charaters) and which is right-justified. The TMaskEdit component allows the numeric mask but does not have an alignment property, nor does the Editbox component. How do I get the right-justification?
Alas, neither the TMaskEdit nor the TEdit components have a property "Alignment". You also can not change the alignment of the TMaskEdit with certain masks, i.e. the mask does not influence the alignment.
Here are a few solutions:
1. Use a TMemo instead of a TMaskEdit, at design time set its alignment property to taRightJustify, and set its Height so that it looks like a TEdit. As a TMemo has no Mask property, you have to program that mask behavior yourself, by filtering the keystrokes in the OnKeyPress event of the TMemo: only allow keys from "0" to "9" and the Backspace key (for erasing). You can also allow a decimal sign if you want numbers with a decimal fraction. Additionally, allow a minus sign if you want negative as well as positive numbers. Caution: watch out for multiple decimal signs or a minus sign that's not in the first position. Seems complicated, but the code is very short.
Note that you also don't want "returns" that would break up the first line of the TMemo. But as a pleasant side effect of the code, the return-keystrokes are filtered out automatically, as well as all the other undesired keys ...since you only *allow* a list of valid keys! That's value for nothing :)
Example: if you want to limit to only positive integer numbers, your event handler is as follows:
procedure TForm1.Memo1KeyPress(Sender: TObject; var Key: Char); begin // Discard key if not numeric, nor a Backspace (code #8) if not (Key in ['0'..'9', #8]) then Key := #0; end;
How to filter for more complex stuff such as the decimal point and/or a minus sign, can be read in our article "Only numerical input in a TEdit" in DelphiLand's source code section ("Code snips").
A possible disadvantage of this solution: a TMemo does not have an AutoSelect property. If that one is absolutely needed, you have to use solution 2 or 3.
2. Install a custom-made TMaskEdit or TEdit, that has an Alignment property. There are several freeware right-aligned TEdits around, even TEdits with an Alignment property that can be changed at design time as well as at runtime (left/ centered/ right). I haven't seen any freeware right-aligned TMaskEdit's, but you easily can filter the keystrokes in the OnKeyPress event as described above.
3. Program your own right-aligned TEdit and install it on the component palette. The code itself is short, but explaining the details would lead us too far in this limited space. This might be an interesting topic for an advanced DelphiLand tutorial, so if you are interested, let me know and I'll start writing :)