Forum > LCL
How do I find a form and component by name
(1/1)
Josh:
Hi
How can I find a component by its name which is on a form by its name.
ie
var frm_name='frm1'; and cmp_name=' pan1';
i have tried code below but the component is not found, the form is found though.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---function FindFormByName(const AName: string): TForm;var i: Integer;begin for i := 0 to Screen.FormCount - 1 do begin Result := Screen.Forms[i]; if (Result.Name = AName) then Exit; end; Result := nil;end; ...var frm:tform; pnl:tpanel; begin frm:=FindFormByName('Frm1'); if frm<>nil then begin pnl:=frm.findcomponent('Pan1'); if pnl=nil then showmessage('Pan1 not found on Frm1') else begin .... end;end;
wp:
You say "the component is not found". I think this is not true since you cannot even compile this code. The return value of FindComponent is TComponent, but you assign this to a TPanel variable. This is rejected by the compiler because a TComponent can be "anything", not necessarily a TPanel. Better to work with a TComponent variable, check whether this is a TPanel and cast the TComponent to a TPanel:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var frm:tform; pnl:tpanel; c: TComponent;begin frm:=FindFormByName('Frm1'); if frm<>nil then begin c := frm.findcomponent('Pan1'); if c=nil then showmessage('Pan1 not found on Frm1') else if not (c is TPanel) then showmessage('Pan1 found on Frm1, but it is not a TPanel') else begin pnl := TPanel(c); .... end;end;
Navigation
[0] Message Index