Solved

align edit text

Posted on 2002-07-25
9
1,101 Views
Last Modified: 2010-04-04
how can i align the text of an edit to center of the edit instead of aligning it to the left ?
0
Comment
Question by:drnadeem
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7176997
you must create a new component,
alignment is not supported by the standard TEdit

following a sample code from barry (inthe) for a new
object based on TEdit with alignment-property

unit AlignEdit;

interface

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

type
 TEditAlign = class(TEdit)
 private
   { Private declarations }
 f_alignment: TAlignment;
 protected
   { Protected declarations }
   procedure CreateParams(var params:TCreateParams); override;
 procedure SetAlignment(value: TAlignment);
public
   { Public declarations }
constructor Create(AOwner: TComponent); override;
 published
   { Published declarations }
property Alignment:TAlignment read f_alignment write SetAlignment;

 end;

procedure Register;

implementation
constructor TEditAlign.Create(AOwner: TComponent);
begin
 inherited create(AOwner);
 end;

procedure TEditAlign.SetAlignment(value: TAlignment);
begin
 if value<>f_alignment then begin
   f_alignment:=value;
   recreatewnd;
   end;
 end;

procedure TEditAlign.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[F_Alignment] or ES_MULTILINE;
 end;
procedure Register;
begin
 RegisterComponents('Samples', [TEditAlign]);
end;

hope this helps

meikl ;-)
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7177002
You can choose between two solutions:

1. Create your own custom edit, and override its CreateParams method, like this:

procedure TmyEdit.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  case FAlignment of
    taLeftJustify  : ;
    taRightJustify : Params.Style := Params.Style or ES_RIGHT;
    taCenter       : Params.Style := Params.Style or ES_CENTER;
  end;
end;


2. Center the text 'by hand', like this:

procedure CenterEdit(e: TEdit);
var w : integer;
begin
  w := Form1.Canvas.TextWidth(e.Text);
  e.Perform(EM_SETMARGINS, EC_LEFTMARGIN, (e.Width - w) div 2);
end;

Call this function from wherever you like, for example:

procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  CenterEdit(Form1.Edit1);
end;

procedure TForm1.Edit1Enter(Sender: TObject);
begin
  CenterEdit(Form1.Edit1);
end;
0
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 50 total points
ID: 7181107
Hello

>>you must create a new component,
Meikl, you could do that without creating new comonent

redeclaring the TEdit class with new property will overide the TEdit class, and then you can simply add new property without creating new component

here's the example, it's similar with Meikl example, but without force you to create new component

unit Unit1;

interface

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

type
  TEdit = class(StdCtrls.TEdit)
  private
    FAlignment : TAlignment;
    procedure SetAlignment(Value: TAlignment);
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  public
    property Alignment: TAlignment read FAlignment write SetAlignment;
end;
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TEdit.CreateParams(var Params: TCreateParams);
const
  Alignments : array[TAlignment] of LongWord= (ES_Left,ES_Right, ES_Center);
begin
  inherited CreateParams(Params);
  Params.Style := Params.Style or Alignments[FAlignment];
end;

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

procedure TForm1.Button1Click(Sender: TObject);
begin
 Edit1.Alignment := taRightJustify;
end;

Best regards
Mohammed Nasman
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7181116
Hello

>>you must create a new component,
Meikl, you could do that without creating new comonent

redeclaring the TEdit class with new property will overide the TEdit class, and then you can simply add new property without creating new component

here's the example, it's similar with Meikl example, but without force you to create new component

unit Unit1;

interface

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

type
  TEdit = class(StdCtrls.TEdit)
  private
    FAlignment : TAlignment;
    procedure SetAlignment(Value: TAlignment);
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  public
    property Alignment: TAlignment read FAlignment write SetAlignment;
end;
type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TEdit.CreateParams(var Params: TCreateParams);
const
  Alignments : array[TAlignment] of LongWord= (ES_Left,ES_Right, ES_Center);
begin
  inherited CreateParams(Params);
  Params.Style := Params.Style or Alignments[FAlignment];
end;

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

procedure TForm1.Button1Click(Sender: TObject);
begin
 Edit1.Alignment := taRightJustify;
end;

Best regards
Mohammed Nasman
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 7181122
sorry for duplicated comments, connection problem ;)

I forgot that you want to center alignment

just change the property Alignment to
Edit1.Alignment := taCenter
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7181161
nice trick, Mohammed ;-)
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7181173
:)
Yes, very nice!
0
 
LVL 1

Author Comment

by:drnadeem
ID: 7181663
Thank you. It has been most helpful.
0
 
LVL 8

Expert Comment

by:Cerf
ID: 11573773
Would it be bad if I use a TMemo instead and make it look like a TEdit?
Is it wrong?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now