如何将表单作为参数传递给过程并操作其属性?

发布于 2024-10-07 03:07:29 字数 166 浏览 1 评论 0原文

假设我想在我制作的单元中创建一个过程,该过程按名称显示和隐藏表单(作为参数传递)。

我怎样才能做到这一点以及语法是什么?

谢谢。

编辑

我正在寻找类似的东西:Popup(FormMy, 'Show');从我的单位内部。

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.

EDIT

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

望喜 2024-10-14 03:07:29

您可以编写这样的过程

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

,并通过 ShowMyForm(MyForm, True); 调用您的表单,并确保您的单元使用 Forms

正如 David 所说,您可以做到这一点

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

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;
真心难拥有 2024-10-14 03:07:29

假设表单是使用父表单作为所有者 [.Create(Self)] 创建的,这应该有效:

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;

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;
莫相离 2024-10-14 03:07:29

您可以循环全局 Screen 对象的 CustomForms 属性(有 CustomFormCount 个)。这只是枚举了应用程序中的所有 VCL 表单,这可能是您想要的。

如果你正在寻找代码,它会是这样的:

for i := 0 to Screen.CustomFormCount-1 do begin
  Form := Screen.CustomForms[i];
  if Form.Name=TargetName then begin
    DoSomething(Form);
    break;
  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;
懒的傷心 2024-10-14 03:07:29
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;
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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文