Reputation: 1015
I'm using turbo c++ explorer edition (the free edition). Is there somebody that know how i can set the textAlignment of a TEdit control?
Upvotes: 2
Views: 7063
Reputation: 1275
I have also make able to be all in another separated unit, so here it is:
unit AlignedTEdit;
interface
uses Windows,Classes,Controls,StdCtrls;
type
TEdit=class(StdCtrls.TEdit)
private
FAlignment:TAlignment;
procedure SetAlignment(Value:TAlignment);
protected
procedure CreateParams(var Params:TCreateParams);override;
public
constructor Create(AOwner:TComponent);override;
published
property Alignment:TAlignment read FAlignment write SetAlignment default taLeftJustify;
end;
implementation
procedure TEdit.SetAlignment(Value:TAlignment);
begin
if FAlignment<>Value
then begin
FAlignment:=Value;
RecreateWnd;
end;
end;
procedure TEdit.CreateParams(var Params:TCreateParams);
const
Alignments:array[TAlignment] of Cardinal=(ES_LEFT,ES_RIGHT,ES_CENTER);
begin
inherited CreateParams(Params);
Params.Style:=Params.Style or Alignments[FAlignment];
end;
constructor TEdit.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FAlignment:=taLeftJustify;
end;
end.
This is a whole unit, save it to a file called AlignedTEdit.pas
.
Then on any form you have a TEdit
add ,AlignedTEdit
at the end of the interface uses clause.
P.D.: The same idea is possible to be done for TStringGrid
, just search on stackoverflow.com for TStringGrid.SetCellsAlignment
or read post Delphi: How to make cells' texts in TStringGrid center aligned?
Upvotes: 0
Reputation: 1275
On Delphi i do it by overloading TEdit type, in this way:
On interface section, before any TForm declaration i put:
type
TEdit=class(StdCtrls.TEdit)
private
FAlignment:TAlignment;
procedure SetAlignment(Value:TAlignment);
protected
procedure CreateParams(var Params:TCreateParams);override;
public
constructor Create(AOwner:TComponent);override;
published
property Alignment:TAlignment read FAlignment write SetAlignment default taLeftJustify;
end;
On the implementation section i put the implementation for such:
procedure TEdit.SetAlignment(Value:TAlignment);
begin
if FAlignment<>Value
then begin
FAlignment:=Value;
RecreateWnd;
end;
end;
procedure TEdit.CreateParams(var Params:TCreateParams);
const
Alignments:array[TAlignment] of Cardinal=(ES_LEFT,ES_RIGHT,ES_CENTER);
begin
inherited CreateParams(Params)
Params.Style:=Params.Style or Alignments[FAlignment];
end;
constructor TEdit.Create(AOwner:TComponent);
begin
inherited Create(AOwner);
FAlignment:=taLeftJustify;
end;
Then on the form OnCreate
event i put something like this:
MyEditBox.Alignment:=taLeftJustify;
MyEditBox.Alignment:=taCenter;
MyEditBox.Alignment:=taRightJustify;
That is it.
Pelase note it can be improved a lot, it is just a proof-of-concept of adding it to TEdit, it is not about creating a new class (with other name) and neither it is about creating a new component.
Hope this can be usefull to someone.
P.D.: The same idea is possible to be done for TStringGrid, just search on stackoverflow.com for CellsAlignment
or read post Delphi: How to make cells' texts in TStringGrid center aligned?
Upvotes: 3
Reputation: 7230
You might find this solution to the problem interesting:
http://bcbjournal.com/bcbcaq/?loc=edits&caq=28
It makes the edit box right aligned by enabling the ES_RIGHT
Windows style for the control, however it does this when creating the component. For historical reasons the standard windows edit control does not support changing alignment after it has been created (officially that is) as mentioned in this post on The Old New Thing. However as you can tell from examining various claims and comments this has changed and though still undocumented should be possible.
So if you want to do this without creating your own component you can use the Windows API function SetWindowLong
like this:
DWORD alignment = ES_RIGHT;
DWORD oldStyle = GetWindowLong(Edit1->Handle, GWL_STYLE);
DWORD newStyle = (oldStyle & ~(ES_LEFT | ES_CENTER | ES_RIGHT)) | alignment;
SetWindowLong(Edit1->Handle, GWL_STYLE, newStyle);
Please note you might have to call SetWindowPos
for the changes to take effect, as noted in the comments in the post linked earlier in the text.
Upvotes: 3
Reputation: 94859
To set the alignment property - which displays the text either left, center or right aligned, you set the Alignment property, e.g. for an edit control called Edit1, which is a pointer member of the form object, to set the alignment to right-justification, you would use:
Edit1->Alignment = taRightJustify;
the other justification properties are taLeftJustify and taCenter.
This only affects the location of the characters on the control, not if it's right-to-left or left-to-right aligned. That can be adjusted by setting the BiDiMode property - bdLeftToRight, bdRightToLeft (these are more important in left-to-right and right-to-left languages)
Or am I missing something obvious in the question?
Upvotes: 2