Search code examples
delphidelphi-7

How to center a text of an edit box in Delphi7?


I am currently working on a BlackJack application in Delphi7 and I am trying to center the text of an edit box in order to later show the card value. I found this documentation (http://delphidabbler.com/tips/85) and now I am failing to properly implement it. I put the code from the link into "Unit2" and am now trying to call both functions on my edit boxes from "Unit1" to align their text. Whenever i try to call one of both functions it tells me that passed parameters aren't identical. If you guys would be able to help me out, it would be much appreciated.

Here is the decleration of Unit1:

 unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button10: TButton;
    Button4: TButton;
    Edit2: TEdit;
    Edit3: TEdit;

[...]

Here the code of Unit2:

unit Unit2;

interface

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

type
  TMyEdit = Class(TEdit)
  public
    FAlignment: TAlignment;
    procedure SetAlignment(Value: TAlignment);
    procedure CreateParams(var Params: TCreateParams); override;
    property Alignment: TAlignment read FAlignment write SetAlignment;
  end;

implementation

procedure TMyEdit.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  case Alignment of
    taLeftJustify:
      Params.Style := Params.Style or ES_LEFT and not ES_MULTILINE;
    taRightJustify:
      Params.Style := Params.Style or ES_RIGHT and not ES_MULTILINE;
    taCenter:
      Params.Style := Params.Style or ES_CENTER and not ES_MULTILINE;
  end;
end;

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

Solution

  • You are not actually using the TMyEdit class at all. That is why Unit1 can't use the functionality of Unit2. Unit1 is still using the standard TEdit.

    You have two choices:

    1. Move Unit2 to its own package that registers TMyEdit, and then install that package into the IDE. TMyEdit will then be available at design-time, and you can replace the TEdit controls with TMyEdit controls.

    2. If you don't want to go that route, the alternative is to redeclare TMyEdit to TEdit and leave Unit1 as-is. It will use the last TEdit type declared in the uses clause. This is known as an "interposer class", eg:

      unit Unit2;
      
      interface
      
      uses
        Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
        Dialogs, StdCtrls;
      
      type
        TEdit = Class(StdCtrls.TEdit)
        public
          FAlignment: TAlignment;
          procedure SetAlignment(Value: TAlignment);
          procedure CreateParams(var Params: TCreateParams); override;
          property Alignment: TAlignment read FAlignment write SetAlignment;
        end;
      
      implementation
      
      procedure TEdit.CreateParams(var Params: TCreateParams);
      begin
        inherited CreateParams(Params);
        case Alignment of
          taLeftJustify:
            Params.Style := Params.Style or ES_LEFT and not ES_MULTILINE;
          taRightJustify:
            Params.Style := Params.Style or ES_RIGHT and not ES_MULTILINE;
          taCenter:
            Params.Style := Params.Style or ES_CENTER and not ES_MULTILINE;
        end;
      end;
      
      procedure TEdit.SetAlignment(Value: TAlignment);
      begin
        if FAlignment <> Value then
        begin
          FAlignment := Value;
          RecreateWnd;
        end;
      end;
      
      end.