I need to know the basics of creating a component and managing subcomponents. First I tried this by creating a TCollection and tried to put a name in every TCollectionItem . But I found out that it is not as simple as I had hoped.
So now I'm going to start this project from scratch again, and this time I want to do everything right. These subcomponents are not visual components and should not have any display or window, only based on TComponent . The main component containing these subcomponents will also be based on TComponent . So nothing here is visual at all, and I don’t want to have a small icon in my form (during development) for each of these subcomponents.
I would like to be able to maintain and manage these subcomponents as a collection. The important thing is that these subcomponents must be created, named and added to the form source, such as menu items. That is the whole point of the idea, first of all, if they cannot be called, then this whole idea is kaput.
Oh, one more important thing: the main component that is the parent of all subcomponents should be able to save these subcomponents in a DFM file.
Example:
Instead of accessing one of these sub-elements, for example:
MyForm.MyItems[1].DoSomething();
Instead, I would like to do something like:
MyForm.MyItem2.DoSomething();
Therefore, I do not need to rely on the identifier of each element.
EDIT:
I felt that I needed to include my original code a bit so that I could see how the original collection works. Here, only the server-side data collection and collection is removed from the full block:
// Command Collections // Goal: Allow entering pre-set commands with unique Name and ID // Each command has its own event which is triggered when command is received // TODO: Name each collection item as a named component in owner form //Determines how commands are displayed in collection editor in design-time TJDCmdDisplay = (cdName, cdID, cdCaption, cdIDName, cdIDCaption); TJDScktSvrCmdEvent = procedure(Sender: TObject; Socket: TJDServerClientSocket; const Data: TStrings) of object; TSvrCommands = class(TCollection) private fOwner: TPersistent; fOnUnknownCommand: TJDScktSvrCmdEvent; fDisplay: TJDCmdDisplay; function GetItem(Index: Integer): TSvrCommand; procedure SetItem(Index: Integer; Value: TSvrCommand); procedure SetDisplay(const Value: TJDCmdDisplay); protected function GetOwner: TPersistent; override; public constructor Create(AOwner: TPersistent); destructor Destroy; procedure DoCommand(const Socket: TJDServerClientSocket; const Cmd: Integer; const Data: TStrings); function Add: TSvrCommand; property Items[Index: Integer]: TSvrCommand read GetItem write SetItem; published property Display: TJDCmdDisplay read fDisplay write SetDisplay; property OnUnknownCommand: TJDScktSvrCmdEvent read fOnUnknownCommand write fOnUnknownCommand; end; TSvrCommand = class(TCollectionItem) private fID: Integer; fOnCommand: TJDScktSvrCmdEvent; fName: String; fParamCount: Integer; fCollection: TSvrCommands; fCaption: String; procedure SetID(Value: Integer); procedure SetName(Value: String); procedure SetCaption(const Value: String); protected function GetDisplayName: String; override; public procedure Assign(Source: TPersistent); override; constructor Create(Collection: TCollection); override; destructor Destroy; override; published property ID: Integer read fID write SetID; property Name: String read fName write SetName; property Caption: String read fCaption write SetCaption; property ParamCount: Integer read fParamCount write fParamCount; property OnCommand: TJDScktSvrCmdEvent read fOnCommand write fOnCommand; end; //////////////////////////////////////////////////////////////////////////////// implementation //////////////////////////////////////////////////////////////////////////////// { TSvrCommands } function TSvrCommands.Add: TSvrCommand; begin Result:= inherited Add as TSvrCommand; end; constructor TSvrCommands.Create(AOwner: TPersistent); begin inherited Create(TSvrCommand); Self.fOwner:= AOwner; end; destructor TSvrCommands.Destroy; begin inherited Destroy; end; procedure TSvrCommands.DoCommand(const Socket: TJDServerClientSocket; const Cmd: Integer; const Data: TStrings); var X: Integer; C: TSvrCommand; F: Bool; begin F:= False; for X:= 0 to Self.Count - 1 do begin C:= GetItem(X); if C.ID = Cmd then begin F:= True; try if assigned(C.fOnCommand) then C.fOnCommand(Self, Socket, Data); except on e: exception do begin raise Exception.Create( 'Failed to execute command '+IntToStr(Cmd)+': '+#10+e.Message); end; end; Break; end; end; if not F then begin //Command not found end; end; function TSvrCommands.GetItem(Index: Integer): TSvrCommand; begin Result:= TSvrCommand(inherited GetItem(Index)); end; function TSvrCommands.GetOwner: TPersistent; begin Result:= fOwner; end; procedure TSvrCommands.SetDisplay(const Value: TJDCmdDisplay); begin fDisplay := Value; end; procedure TSvrCommands.SetItem(Index: Integer; Value: TSvrCommand); begin inherited SetItem(Index, Value); end; { TSvrCommand } procedure TSvrCommand.Assign(Source: TPersistent); begin inherited; end; constructor TSvrCommand.Create(Collection: TCollection); begin inherited Create(Collection); fCollection:= TSvrCommands(Collection); end; destructor TSvrCommand.Destroy; begin inherited Destroy; end; function TSvrCommand.GetDisplayName: String; begin case Self.fCollection.fDisplay of cdName: begin Result:= fName; end; cdID: begin Result:= '['+IntToStr(fID)+']'; end; cdCaption: begin Result:= fCaption; end; cdIDName: begin Result:= '['+IntToStr(fID)+'] '+fName; end; cdIDCaption: begin Result:= '['+IntToStr(fID)+'] '+fCaption; end; end; end; procedure TSvrCommand.SetCaption(const Value: String); begin fCaption := Value; end; procedure TSvrCommand.SetID(Value: Integer); begin fID:= Value; end; procedure TSvrCommand.SetName(Value: String); begin fName:= Value; end;
delphi custom-component delphi-7 tcollection tcollectionitem
Jerry dodge
source share