Solved

align edit text

Posted on 2002-07-25
9
1,116 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Working this component WebCopy with proxy, help 3 92
LAN or WAN ? 11 95
Delphi: Connect to running MS Outlook 4 118
Delphi...Split view - idea? 1 88
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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

839 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