Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Center align the text of a TEdit?

Status
Not open for further replies.

BobbaFet

Programmer
Feb 25, 2001
903
NL
How would I achieve this? (I would explain more carefully
then just the subject, but that's really all I want)

Thanks a lot,

BobbaFet

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 

Can you use a TStaticText instead? (It has an Alignment property you can set to taCenter.)

 
not sure where I got this code from, but it's not mine: Probably from torry.net somewhere

Code:
unit AlignEdit;

{$R-}

interface

uses
  Messages, Windows, SysUtils, Classes, StdCtrls, Controls, Graphics, Forms;

type
  TAlignEdit = class(TEdit)
  private
    FAlignment: TAlignment;
  protected
    procedure CreateParams(var Params: TCreateParams); override;
    procedure SetAlignment(Value: TAlignment);
  public
    constructor Create(AOwner: TComponent); override;
  published
    property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
  end;

procedure Register;

implementation

constructor TAlignEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FAlignment := taLeftJustify;
end;

procedure TAlignEdit.CreateParams(var Params: TCreateParams);
const
  Alignments : array[TAlignment] of Longint = (ES_LEFT, ES_RIGHT, ES_CENTER);
begin
  inherited CreateParams(Params);
    Params.Style := Params.Style or Alignments[FAlignment];
end;

procedure TAlignEdit.SetAlignment(Value: TAlignment);
begin
  if FAlignment <> Value then
  begin
    FAlignment := Value;
    RecreateWnd;
  end;
end;

procedure Register;
begin
  RegisterComponents('Additional', [TAlignEdit]);
end;

end.
 
That came from swissdelphicenter.ch, I saw it when I googled for it, but I can't get it to work as just code in
stead of a new component.

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
I've never been able to either. The CreateParams procedure only seems to be called once - on the component creation. It seems as if the standard TEdit control simply doesn't support alignment at all, nor does it support custom drawing. I can't see a way around it, unless you're using a monospaced font.
 
Seems you forgot a part though:
Code:
unit AlignEdit;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type

  TAlign = (eaLeft, eaCenter, eaRight);

  TAlignEdit = class(TEdit)
  private
    { Private-Deklarationen }
    FAlign: TAlign;
    procedure SetAlign(const Value: TAlign);
  protected
    { Protected-Deklarationen }
    procedure CreateParams(var Params: TCreateParams); override;
  public
    { Public-Deklarationen }
    constructor Create(AOwner: TComponent); override;
  published
    { Published-Deklarationen }
    property Alignment: TAlign read FAlign write SetAlign default eaLeft;
  end;

procedure Register;

implementation

constructor TAlignEdit.Create(Aowner: TComponent);
begin
  inherited Create(AOwner);
  FAlign := eaLeft;
end;

procedure TAlignEdit.SetAlign(const Value: TAlign);
begin
  if FAlign <> Value then
  begin
    FAlign := Value;
    RecreateWnd;
  end;
end;

procedure TAlignEdit.CreateParams(var Params: TCreateParams);
begin
  inherited;
  case FAlign of
    eaLeft: Params.Style   := Params.Style or ES_LEFT;
    eaCenter: Params.Style := Params.Style or ES_CENTER;
    eaRight: Params.Style  := Params.Style or ES_RIGHT;
  end;
end;

procedure Register;
begin
  RegisterComponents('SwissDelphiCenter', [TAlignEdit]);
end;

end.

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
well, actually no I didn't. The TAlignment class is already declared in the Classes unit, so there was no need to redeclare that.
 
I thought TAlignment arranged the alignment of the component itself, not as in text align ???

[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
nope. If you have a look at the declaration for TAlignment, it only allows for taLeftJustify, taRightJustify, taCenter.

TAlign allows for alNone, alTop, alBottom, alLeft, alRight, alClient, alCustom and is used for control alignment within the form.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top