PDA

View Full Version : Frames maintenance



tanffn
01-07-2006, 12:56 PM
Never find the need to use Frames, and now that I want to use it I have the feeling im over complicating things :?

The idea is to have a datalist that is imbedded in one of the forms and also allow the option to separate it so it will be in a stand alone window.
So I create a Frame with all the GUI elements I wanted and I included it inside the main app Form and in a new Form. To control the data I used the following code:


TMyFrame = Class(TFrame)
Controller: TController
end;

TMyFrame.Add(S: String)
Controller.Add(S)
end;

TMyFrame.OnControllerChangeEvent
Repaint;
end;

...

TController.Add(S: String)
DB.Add(S)
NotifyAllMembers(OnControllerChangeEvent)
end;

What do you think, do you have a better idea for solving this issue?
Note that in this case I won't mind having only 1 "copy" of the window, maybe I should make is as a dockable form..? had bad experience with that...

tanffn
05-07-2006, 08:41 AM
As there were no post I just wrote my initial idea, maybe its not the best way to do this but it works :)


unit SongListManagerUnit;

interface
uses
Classes;

type
TListUpdateEvent = procedure;

TSongListManager = class
private
FSongList: TStrings;
FFrameList: TList;
FFrameEvent: TList;

procedure SendUpdateEvent(Sender: TObject);
public
constructor Create;
procedure Free;

procedure LoadList(path: string);
procedure SaveList(path: string);

procedure AddFrame(const item: TObject;
const UpdateEventHandler: TListUpdateEvent);
procedure AddData(data: string; Sender: TObject);
procedure DeleteData(Index: Integer; Sender: TObject);

property SongList: TStrings read FSongList;
end;

var
SongListManager: TSongListManager;

implementation

constructor TSongListManager.Create;
begin
inherited Create;
FSongList:= TStringlist.Create;
FFrameList:= TList.Create;
FFrameEvent:= TList.Create;
end;

procedure TSongListManager.Free;
begin
FSongList.Free;
inherited;
end;

procedure TSongListManager.AddFrame(const item: TObject;
const UpdateEventHandler: TListUpdateEvent);
begin
FFrameList.Add(item);
FFrameEvent.Add(@UpdateEventHandler);
end;

procedure TSongListManager.AddData(data: string; Sender: TObject);
begin
FSongList.Append(data);
SendUpdateEvent(Sender);
end;

procedure TSongListManager.DeleteData(Index: Integer; Sender: TObject);
begin
FSongList.Delete(Index);
SendUpdateEvent(Sender);
end;

procedure TSongListManager.SendUpdateEvent(Sender: TObject);
var
I: Integer;
begin
for I:= 0 to FFrameEvent.Count - 1 do
if Sender <> FFrameList[I] then
if Assigned(FFrameEvent[I]) then
TListUpdateEvent(FFrameEvent[I]);
end;

procedure TSongListManager.LoadList(path: string);
begin
FSongList.LoadFromFile(path);
end;

procedure TSongListManager.SaveList(path: string);
begin
FSongList.SaveToFile(path);
end;

Initialization
SongListManager:= TSongListManager.Create;

finalization
SongListManager.Free;

end.