Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

align edit text

Posted on 2002-07-25
9
Medium Priority
?
1,204 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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 this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Loops Section Overview
Suggested Courses

610 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