动态列出项目中的所有表单 [英] Dynamically list all forms in a project

查看:62
本文介绍了动态列出项目中的所有表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想动态列出项目中存在于我的项目中的所有表单的名称,然后通过单击它们中的每一个,列出存在于另一个ListBox中该表单的所有按钮。

I want to list name of all forms exist in my project in a ListBox Dynamically, then by clicking on each of them, list all buttons exist on that form in another ListBox.

但是我不知道它是否可以实现以及如何实现。

But I don't know if it can be implemented and how it can.

请帮助我。

谢谢

推荐答案

sabri.arslan的答案是查找所有实例化实例的方法

sabri.arslan's answer is the way to go to find all instantiated forms at run-time.

在评论中,哈米德(Hamid)寻求一种寻找未分配形式的方法。假设未分配,他表示未实例化的形式,那么只有一种方法,那就是遍历vcl流系统使用的类的注册表,以便在流dfm时按名称实例化组件。

In the comments Hamid asked for a way to find unassigned forms as well. Assuming that by unassigned he means uninstantiated forms, there would be only one way to do so and that is to iterate over the registry of classes used by the vcl streaming system to instantiate components by name when a dfm is streamed in.

但是,IIRC表单不会自动添加到注册表中。实际上,如果您要基于其名称的字符串实例化表单,则需要自己将其添加到类注册表中。当然,OP可以为他自己项目中的每个表单执行此操作。但是,这留下了一个问题,即流系统使用的类注册表是使用class单元的实现部分中的var实现的。因此不能从外部(轻松)进行迭代。

However, IIRC, forms are not automatically added to the registry. In fact, if you want to instantiate forms based on a string of their name, you need(ed) to add them to the class registry yourself. OP could of course do that for each of the forms in his project himself. But, that leaves the problem that the class registry used by the streaming system is implemented using var's in the implementation section of the classes unit. And therefore can't be iterated over (easily) from the outside.

所以解决方案是使用项目中所有表单单元的初始化部分并注册每个表单单元您自己的注册表中的表单及其名称和类,并让注册表提供迭代已注册表单的方法。这些方法可用于填充OP提到的列表框。

So the solution would be to use the initialization section of all form units in the project and register each form in a "roll-your-own" registry with their name and class and have the registry provide the methods to iterate over the registered forms. These method can be used to populate the listbox mentioned by the OP.

要在窗体上使用TButton,则需要实例化窗体(它可能保持隐藏状态),并且使用类似于sabri.arslan的答案的代码遍历组件以找到TButton实例。

To get at the TButtons on a form would then require instantiating the form (it could remain hidden) and iterating over the components using code similar to sabri.arslan's answer to find the TButton instances.

实例化表单将需要根据注册表从注册表中获取表单的类。在列表框中选择表单的名称。

Instantiating the form would require getting the class of the form from the registry based on the form's name selected in the listbox.

简单的自备表单注册表示例:

Example of a simple roll-your-own form registry:

unit Unit1;

interface

uses
  Classes
  , Forms
  , SysUtils
  ;

  procedure RegisterForm(aName: string; aClass: TFormClass);
  procedure ListForms(aNames: TStrings);
  function InstantiateForm(aName: string): TCustomForm;

implementation

var
  FormRegistry: TStringList;

procedure RegisterForm(aName: string; aClass: TFormClass);
begin
  FormRegistry.AddObject(aName, Pointer(aClass));
end;

procedure ListForms(aNames: TStrings);
var
  i: Integer;
begin
  for i := 0 to FormRegistry.Count - 1 do begin
    aNames.Add(FormRegistry[i]);
  end;
end;

function InstantiateForm(aName: string): TCustomForm;
var
  idx: Integer;
  frmClass: TFormClass;
begin
  Result := nil;
  idx := FormRegistry.IndexOf(aName);
  if idx > -1 then begin
    frmClass := TFormClass(FormRegistry.Objects[idx]);
    Result := frmClass.Create(nil);
  end;
end;

initialization
  FormRegistry := TStringList.Create;
  FormRegistry.Duplicates := dupError;
  FormRegistry.Sorted := True;
finalization
  FreeAndNil(FormRegistry);
end.

这篇关于动态列出项目中的所有表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆