Delphi component to show some text file at design time
Posted by jpluimers on 2019/02/19
Interesting idea: [WayBack] Delphi component to show some text file at design time
Via: [WayBack] On some projects I put an invisible TMemo on the main form to keep notes about things todo…is there any extension that could attach a kind …could save to … – Paul TOTH – Google+
–jeroen
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. |
Leave a Reply