2

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?

Johan
  • 74,508
  • 24
  • 191
  • 319
BlackShadow
  • 1,005
  • 6
  • 19
  • 25
  • Note that the turbo c++ explorer is the free version of c++builder and has nothing to do with turbo-c++ of old. – Johan Jun 10 '14 at 13:29

4 Answers4

3

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?

Community
  • 1
  • 1
z666zz666z
  • 1,235
  • 14
  • 7
3

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.

Tommy Andersen
  • 7,165
  • 1
  • 31
  • 50
  • SetWindowPos didn't do the trick, but calling Edit1->Invalidate worked for me. As Raymond says, this is an unsupported hack... – James Johnston Sep 14 '15 at 16:27
2

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?

Anya Shenanigans
  • 91,618
  • 3
  • 107
  • 122
  • 1
    In turbo c++ explorer, there is no 'Alignment' propriety for the TEdit component... – BlackShadow Dec 15 '10 at 22:38
  • I'll just set up a virtual machine and see what's needed – Anya Shenanigans Dec 16 '10 at 09:40
  • Finding the demo edition of turbo c++ explorer is not trivial – Anya Shenanigans Dec 16 '10 at 09:52
  • The free version is no longer available: ( – BlackShadow Dec 16 '10 at 14:25
  • Older versions of VCL did not have an Alignment property on the TEdit control. Newer versions do. – James Johnston Sep 14 '15 at 16:28
  • @JamesJohnston that would explain the confusion I had. I was never able to find a suitable copy of the compiler and my answer just languished. – Anya Shenanigans Sep 14 '15 at 16:32
  • @Petesh: I just did some more searching on the subject and apparently Windows 95 and Windows NT 4.0 did not support ES_RIGHT or ES_CENTER on an edit control that was not also ES_MULTILINE. The old VCL design of only providing an Alignment property on the TMemo control made sense on those platforms. Windows 98 and later Windows NT 4.0 service packs added support for alignment of non-multiline edit controls, making the old VCL design of only having Alignment on TMemo seem silly. – James Johnston Sep 14 '15 at 16:43
0

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?

Community
  • 1
  • 1
z666zz666z
  • 1,235
  • 14
  • 7