Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Delphi component to show some text file at design time
unit uDesignNote;
interface
uses
System.SysUtils, System.Classes, Vcl.Forms, Vcl.StdCtrls;
type
TDesignNote = class(TComponent)
private
FContentForm: TForm;
FFileName: string;
function GetContentForm: TForm;
function GetMemo: TMemo;
function GetVisible: Boolean;
procedure HideFileContent;
procedure SetFileName(const Value: string);
procedure SetVisible(const Value: Boolean);
procedure ShowFileContent;
procedure UpdateView;
protected
procedure Notification(AComponent: TComponent; Operation: TOperation);
override;
public
destructor Destroy; override;
property ContentForm: TForm read GetContentForm;
property Memo: TMemo read GetMemo;
published
property FileName: string read FFileName write SetFileName;
property Visible: Boolean read GetVisible write SetVisible;
end;
procedure Register;
implementation
uses
Vcl.Controls, System.IOUtils;
procedure Register;
begin
RegisterComponents('Samples', [TDesignNote]);
end;
destructor TDesignNote.Destroy;
begin
Visible := False;
inherited;
end;
function TDesignNote.GetContentForm: TForm;
var
memo: TMemo;
begin
if FContentForm = nil then begin
FContentForm := TForm.Create(Application);
memo := TMemo.Create(FContentForm);
memo.Parent := FContentForm;
memo.Align := alClient;
end;
result := FContentForm;
end;
function TDesignNote.GetMemo: TMemo;
begin
Result := (ContentForm.Controls[0] as TMemo);
end;
function TDesignNote.GetVisible: Boolean;
begin
Result := (FContentForm <> nil) and FContentForm.Visible;
end;
procedure TDesignNote.HideFileContent;
begin
if FContentForm <> nil then begin
FContentForm.Free;
FContentForm := nil;
end;
end;
procedure TDesignNote.Notification(AComponent: TComponent;
Operation: TOperation);
begin
if (AComponent = FContentForm) and (Operation = opRemove) then begin
FContentForm := nil;
end;
inherited;
end;
procedure TDesignNote.SetFileName(const Value: string);
begin
if FFileName <> Value then
begin
FFileName := Value;
UpdateView;
end;
end;
procedure TDesignNote.SetVisible(const Value: Boolean);
begin
if Value or (FContentForm <> nil) then begin
ContentForm.Visible := Value;
end;
end;
procedure TDesignNote.ShowFileContent;
begin
ContentForm.Show;
if TFile.Exists(FileName) then begin
Memo.Lines.LoadFromFile(FileName);
end
else begin
Memo.Lines.Clear;
end;
end;
procedure TDesignNote.UpdateView;
begin
if csDesigning in ComponentState then begin
ShowFileContent;
end;
end;
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.