How does one test if a component exists in your current application for example if you create a dynamic radiogroup named radiogroup1 how do you check if there already is a component with the name radiogroup1?
First you'll have to make a list of all forms in the application.
Then you'll have to search each form for your component using FindComponent
.
Here's some sample code:
Something like this:
function TForm1.FindMyComponent(Parent: TComponent; Name: string): TComponent;
var
i: integer;
begin
if Parent.ComponentCount = 0 then exit(nil);
Result:= Parent.FindComponent(Name);
if Assigned(Result) then Exit;
for i:= 0 to Parent.ComponentCount do begin
Result:= FindMyComponent(Parent.Components[i], Name);
if Assigned(Result) then Exit;
end; {for i}
end;
If you call it like this:
procedure TForm1.Test;
var
MyRadioGroup: TComponent;
begin
MyRadioGroup:= FindMyComponent(Application, 'RadioGroup1');
....
end;
It will recursively look though all registered forms in the application for your radiogroup. See: http://docwiki.embarcadero.com/Libraries/XE2/en/System.Classes.TComponent.FindComponent
Note that the search is not case sensitive.
Do your own bookkeeping
Of course this code will be quite slow if you're looking for lots of controls in this manner.
Also as David stated it does not make sense to attach names to controls you create programmatically. Its better to just keep a list of the control names in a Dictionary and refer to them in that way.
type
TControlClass = class of TControl;
TForm1 = class(TForm)
private
NewIndex: TDictonary<string, integer>;
AllControls: TDictonary<string, TControl>;
....
function TForm1.AddControl(NewControl: TControl);
var
ClassName: string;
Index: integer;
ControlName: string;
begin
ClassName:= NewControl.ClassName;
if not(NewIndex.TryGetValue(ClassName, Index) then Index:= 0;
Inc(Index);
NewIndex.AddOrSetValue(ClassName, Index);
ControlName:= ControlName + IntToStr(Index);
NewControl.Name:= ControlName; //optional;
AllControls.Add(ControlName, NewControl);
end;
type
TFrameClass=class of TFrame;
procedure TForm1.LoadFrame(CurrentFrame: TFrameClass; Name:String);
var
Reference:TFrameClass;
Instance:TFrame;
begin
Instance:=TFrame(FindComponent(Name));
if (Instance=nil) then
begin
Reference:=TFrameClass(CurrentFrame);
Instance:=Reference.Create(Self);
Instance.Align := alClient;
Instance.Parent := ClientPanel;
end
else
begin
Instance.BringToFront;
end;
end;
procedure TForm1.scGPCharGlyphButton4Click(Sender: TObject);
var FrameInternalDistribution:TFrameInternalDistribution;
begin
LoadFrame(TFrameInternalDistribution, 'FrameInternalDistribution');
end;
procedure TForm1.scGPCharGlyphButton2Click(Sender: TObject);
var
FrameInboxDistribution:TFrameInboxDistribution;
begin
LoadFrame(TFrameInboxDistribution, 'FrameInboxDistribution');
end;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With