Mirror

Right-align the content of a TEdit (Views: 301)


Problem/Question/Abstract:

How to right-align the content of a TEdit

Answer:

Solve 1:

procedure TESBPCSCustomEdit.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  case Alignment of
    taLeftJustify: Params.Style := Params.Style or ES_LEFT;
    taRightJustify: Params.Style := Params.Style or ES_RIGHT or ES_MultiLine;
    taCenter: Params.Style := Params.Style or ES_CENTER or ES_MultiLine;
  end;
  if FReadOnly then
    Params.Style := Params.Style or ES_READONLY;
end;


Solve 2:

unit uEditEx;

interface

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

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

procedure Register;

implementation

{R uEditEx.dcr}

procedure Register;
begin
  RegisterComponents('gate(n)etwork', [TEditEx]);
end;

{ TEditEx }

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

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

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

end.

<< Back to main page