开发者

How can I pass a form to a procedure as parameter and manipulate its properties?

Let's say I want to create a procedure in a unit I made that shows and hide forms by name (passed as parameter).

How could I do that and what's the syntax?

Thanks.

ED开发者_运维百科IT

I'm looking for something like: Popup(FormMy, 'Show'); from inside my Unit.


You can write a procedure like this

procedure ShowMyForm(Form: TForm; Show: Boolean);
begin
  if Show then
    Form.Visible := True
  else
    Form.Visible := False;
end;

and call your form by ShowMyForm(MyForm, True); and be sure that your unit uses Forms

As David said you can make it

procedure ShowMyForm(Form: TForm; Show: Boolean);
begin
    Form.Visible := Show 
end;


Assuming that the forms were created with the parent form as the owner [.Create(Self)], this should work:

procedure ShowFormByName(const ParentForm: TForm; const FormName: String; ShowForm: Boolean);
var
  i: Integer;
begin
  for i := 0 to pred(ParentForm.ComponentCount) do
  begin
    if (ParentForm.Components[i] is TForm) and ParentForm.Components[i].Name = FormName) then
    begin
      if ShowForm then
        TForm(ParentForm.Components[i]).Show
      else
        TForm(ParentForm.Components[i]).Hide;

      Break;
    end;
  end;
end;


You can loop around the CustomForms property (there are CustomFormCount of them) of the global Screen object. This simply enumerates all the VCL forms in the app which may be what you want.

If you are looking for code it would be something like this:

for i := 0 to Screen.CustomFormCount-1 do begin
  Form := Screen.CustomForms[i];
  if Form.Name=TargetName then begin
    DoSomething(Form);
    break;
  end;
end;


function GetFormByName(const FormName: string): TForm;
var
  i : Integer;
begin
  Result := nil;
  for i := 0 to Screen.FormCount - 1 do
  begin
    if SameText(Screen.Forms[i].Name,FormName) then
    begin
      Result := Screen.Forms[i];
      Break;
    end;
  end;
end;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜