动态转换表单 [英] Cast a form dynamically
本文介绍了动态转换表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个程序来关闭所有创建的adoquery并在(任何)表单上打开,问题是我需要对表单进行强制转换以标识组件.如何进行如示例所示的动态投射?
I am writing a procedure to close all adoquery created and open on a (any) form, the problem is I need to do a cast on the form to identify the components. how can I make a dynamic cast like shown in the example?
我需要这样的东西
Procedure OpenADODataSets(Form:TForm;FormType:TClass);
...
...
(Form as FormType).ComponentCount
或
Procedure OpenADODataSets(Form:TForm;FormType:TClass);
...
...
FormType(Form).ComponentCount
这是代码.
Procedure OpenADODataSets(Form:TForm;FormType:TClass);
var
i: integer;
begin
for i:=0 to (Form as FormType).ComponentCount-1 do
if (Form as FormType).Components[i].ClassType=TADOQuery then
if not TADOQuery((Form as FormType).Components[i]).Active then
TADOQuery((Form as FormType).Components[i]).Open;
end;
更新
问题由@Edelcom的答案解决
problem solved with the answer of @Edelcom
Procedure OpenADODataSets(Form:TForm);
var
i: integer;
begin
for i:=0 to Form.ComponentCount-1 do
if Form.Components[i].ClassType=TADOQuery then
if not TADOQuery(Form.Components[i]).Active then
TADOQuery(Form.Components[i]).Open;
end;
推荐答案
为什么需要(作为FormType的表单)(如我所评论).查找了一些我自己的代码,效果很好:
Why do you need the (Form as FormType) (as I commented). Looked up some of my own code, which works fine :
procedure TProgSettingsVisual.FillStandardSaveFilesForForm( const pForm: TForm;);
var i : integer;
x : string;
thisComponent : TComponent;
begin
With pForm do
begin
for i := 0 to ComponentCount-1 do
begin
thisComponent := Components[i];
x := LowerCase(Components[i].ClassName);
if x = 'tovcinifilestore'
then begin
TOvcIniFileStore(Components[i]).IniFileName := FormSaveFile(pForm);
end;
if x = 'tovdformstate'
then begin
...
end;
if x = 'tovccomponentstate'
then begin
...
这篇关于动态转换表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文