Solved

align edit text

Posted on 2002-07-25
9
1,090 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:
mnasman 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:mnasman
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 22

Expert Comment

by:mnasman
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

747 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

16 Experts available now in Live!

Get 1:1 Help Now