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

x
?
Solved

Centered Edit control

Posted on 1997-08-08
10
Medium Priority
?
172 Views
Last Modified: 2010-04-04
I need a centered edit control for an app I am developing. I have found one that centers the text AFTER return is pressed but I want the text centered as its entered. I have been using a richtext edit control with the format property set to center which partly solves the problem but its not ideal... Any suggestions?
0
Comment
Question by:davegreen
[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
10 Comments
 
LVL 3

Expert Comment

by:mirek071497
ID: 1340962
WHich Delphi version ?
0
 

Author Comment

by:davegreen
ID: 1340963
I am developing in Delphi 2 at the moment... but should version matter?
0
 
LVL 1

Expert Comment

by:peter_vc
ID: 1340964
From what I can see from the SDK you can only do that with a multi-line edit control with the ES_CENTER control style.

0
Technology Partners: 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!

 
LVL 1

Expert Comment

by:bcrotaz
ID: 1340965
Or you write your own from scratch.....Naaa.......!
0
 
LVL 4

Expert Comment

by:StevenB
ID: 1340966
 After much searching and testing, we've found the WPTools components to be the best available (for both D2 and D3). You can get them at:

http://members.aol.com/jziersch/index.html

  Hope this helps.

  Steven.
0
 

Author Comment

by:davegreen
ID: 1340967
The WPTools components are overkill, they look very much like the included richtext components. I need an edit control that accepts one line only.

0
 
LVL 5

Expert Comment

by:ronit051397
ID: 1340968
This source is from LLoyd's Help file. Hope it works, didn't check it.

Q:  How do I justify the text in a TEdit?

A:  Here is a custom component that will do the job:

unit EditJust;

interface

uses
  SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Menus, DsgnIntF;

type
  TMyCustomEdit = class (TCustomEdit)
  private
    FAlignment: TAlignment;
    FOldAlignment : TAlignment;
    FTextMargin : integer;
    function CalcTextMargin : integer;
    procedure CMEnter(var Message: TCMEnter); message CM_ENTER;

    procedure CMExit(var Message: TCMExit);   message CM_EXIT;
    procedure WMPaint(var Message: TWMPaint); message WM_PAINT;
    procedure SetAlignment(Value: TAlignment);
  protected
    property Alignment: TAlignment read FAlignment write SetAlignment default taLeftJustify;
  public
    constructor Create(AOwner: TComponent); override;
  end;

  TEditJust = class (TMyCustomEdit)
  published
    property Alignment;
    property AutoSize;
    property BorderStyle;

    property Color;
    property Ctl3D;
    property DragCursor;
    property DragMode;
    property Enabled;
    property Font;
    property HideSelection;
    property ParentColor;
    property ParentCtl3D;
    property ParentFont;
    property ParentShowHint;
    property PopupMenu;
    property ReadOnly;
    property ShowHint;
    property TabOrder;
    property Visible;
    property OnChange;
    property OnClick;
    property OnDblClick;
    property OnDragDrop;

    property OnDragOver;
    property OnEndDrag;
    property OnEnter;
    property OnExit;
    property OnKeyDown;
    property OnKeyPress;
    property OnKeyUp;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
  end;

procedure Register;

implementation

constructor TMyCustomEdit.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FTextMargin := CalcTextMargin;
end;

function TMyCustomEdit.CalcTextMargin : integer;

{
This was borrowed from TDBEdit.  It calculates a pixel
offset from the edge of the control to the text
(a margin) used in the paint routine.
}
var
  DC: HDC;
  SaveFont: HFont;
  I: Integer;
  SysMetrics, Metrics: TTextMetric;
begin
  DC := GetDC(0);
  GetTextMetrics(DC, SysMetrics);
  SaveFont := SelectObject(DC, Font.Handle);
  GetTextMetrics(DC, Metrics);
  SelectObject(DC, SaveFont);
  ReleaseDC(0, DC);
  I := SysMetrics.tmHeight;
  if I > Metrics.tmHeight then I := Metrics.tmHeight;

  Result := I div 4;
end;

procedure TMyCustomEdit.SetAlignment(Value: TAlignment);
begin
  if FAlignment <> Value then begin
    FAlignment := Value;
    Invalidate;
  end;
end;

procedure TMyCustomEdit.CMEnter(var Message: TCMEnter);
begin
  inherited;
  FOldAlignment := FAlignment;
  Alignment := taLeftJustify;
end;

procedure TMyCustomEdit.CMExit(var Message: TCMExit);
begin
  inherited;
  Alignment := FOldAlignment;
end;

procedure TMyCustomEdit.WMPaint(var Message: TWMPaint);

{borrowed from TDBEdit}
{paints the text in the appropriate position}
var
  Width, Indent, Left, I: Integer;
  R: TRect;
  DC: HDC;
  PS: TPaintStruct;
  S: string;
  Canvas: TControlCanvas;
begin
  {let the existing code handle left justify}
  if (FAlignment = taLeftJustify) then begin
    inherited;
    Exit;
  end;

  try
    Canvas := TControlCanvas.Create;
    Canvas.Control := Self;
    DC := Message.DC;
    if DC = 0 then DC := BeginPaint(Handle, PS);

    Canvas.Handle := DC;

    Canvas.Font := Font;
    with Canvas do begin
      R := ClientRect;
      if (BorderStyle = bsSingle) then begin
        Brush.Color := clWindowFrame;
        FrameRect(R);
        InflateRect(R, -1, -1);
      end;
      Brush.Color := Color;
      S := Text;
      Width := TextWidth(S);
      if BorderStyle = bsNone then Indent := 0
      else Indent := FTextMargin;
      if FAlignment = taRightJustify then Left := R.Right - Width - Indent

      else Left := (R.Left + R.Right - Width) div 2;
      TextRect(R, Left, Indent, S);
    end;
  finally
    Canvas.Handle := 0;
    if Message.DC = 0 then EndPaint(Handle, PS);
  end; {try}
end;

procedure Register;
begin
  RegisterComponents ('Samples', [TEditJust] );
end;

end.

{This code came from Lloyd's help file!}
0
 

Author Comment

by:davegreen
ID: 1340969
I had already checked this one out, it centers the text after return is pressed. For visual reasons I want the text centered as its entered by the user.
0
 
LVL 1

Accepted Solution

by:
kyriacos earned 750 total points
ID: 1340970
A nice solution is to use an one-line memo edit setting these properties:
  WantReturns := False;
  Height := 25; (for one line)
  Alignment := taCenter;

After all, the size of the TEdit component was found to be 6k while the size of TMemo is 9k... 3k is not so bad...

0
 

Author Comment

by:davegreen
ID: 1340971
I never gave using a memo control like this a thought... it seems to do the job just fine.. Thanks kyriacos
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
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