[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 480
  • Last Modified:

Right Justify TEdit

I need some souce code examples of Right justification of a TEdit in Delphi 6
Thanks
0
eldraco
Asked:
eldraco
  • 3
  • 2
1 Solution
 
hongjunCommented:
(gotten by expert: inthe)

try installing this:


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;

end.
0
 
eldracoAuthor Commented:
I apologise. I forgot to mention that I do not want to use another component.
Thanks anyway
0
 
hongjunCommented:
I think there is no way to do that :(
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
hongjunCommented:
This is the minimum you can do
http://www.undu.com/Articles/000207c.html

hongjun
0
 
eldracoAuthor Commented:
I tried the article from UNDU but I could not get it to work. The operating system I am using is Windows 2000
0
 
TheRealLokiSenior DeveloperCommented:
it's a pretty ugly workaround anyway, you have to call the RJustify procedure on every change/kerpress.
The only way of actually getting a nice result is to use hongjun's example
note: you do not actually have to install a component, just do it like this
(I don't want points, just helping out)

unit Unit1;

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;


type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    MyRightAlignedEdit: TEditAlign; // define your edit box here
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

IMPLEMENTATION

{$R *.DFM}


// TEDIT Methods
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 RJustifyEdit(var ThisEdit : TEdit);
    var
        Left, Width : Integer;
        GString : String;
        Rgn : TRect;
        TheCanvas : TControlCanvas;
    begin
        TheCanvas := TControlCanvas.Create;
        try
            TheCanvas.Control := ThisEdit;
            GString := ThisEdit.Text;
            Rgn     := ThisEdit.ClientRect;
            TheCanvas.FillRect(Rgn);
            Width   := TheCanvas.TextWidth(GString);
            Left := Rgn.Right - Width - 1;
            TheCanvas.TextRect(Rgn, Left, 0, GString);
        finally
            TheCanvas.Free;
            end ;
        end;

// TEDIT Methods - End

procedure TForm1.FormCreate(Sender: TObject);
    begin
// Create your edit box here, it's really quite simple
        MyRightAlignedEdit := TEditAlign.Create(self);
        with MyRightAlignedEdit do
        begin
            Left := 152;
            Top := 56;
            Width := 121;
            Height := 21;
            TabOrder := 0;
            Text := 'RightAlignedEdit';
            Alignment := taRightJustify; // this is the bit you want
            Parent := Form1; // so you can actually see it :)  make the parent a panel or groupbox if you want it there
        end;
       
    end;

end.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now