Mega Code Archive

 
Categories / Delphi / VCL
 

Using Local Class Descendants to Align TEdits

Title: Using Local Class Descendants to Align TEdits Question: A common solution to accessing protected methods and properties is to provide a local descendant of the class. Another use of descendant classes is for accessing properties that have not been published. This example shows a usefull way of getting a TEdit aligned without modifing your vcl. Answer: Lets take the TEdit component. TEdit does not have a published Align property although it is implemented as part of TControl. We have various options to access this Align property. 1) Change the VCL source and recompile (pain in the arse!!) 2) Create a new component based on TEdit that has the property published. (This will work fine, but means you'll have to use your component instead of TEdits) 3) Do the following Declare a new TEdit descendant class in the implementation section as shown below. In the form create cast the Tedit and set the align property to the value you want. unit MyForm; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.DFM} type TAlignedEdit = class(TEdit) published property Align; end; procedure TForm1.FormCreate(Sender: TObject); begin TAlignedEdit(Edit1).Align := alClient; end; end. That's it. I know in some cases it might make more sense to implement your own component to do this, but sometimes it's easier to approach it this way.