Art*_*dio -2 delphi components class
我在下面有这个函数使用ComponetCount,当我在另一个Form中使用这个函数时,它在Form1中,在Form1中使用Form2,它带来了Form1而不是Form2的Componetes数量,我该如何解决这个问题问题?遵循功能:
function Form1.getgridId(KeyField: String): string;
var
i: Integer;
id: string;
begin
for i := 0 to ComponentCount -1 do
begin
if Components[i] is TCustomDBGrid then
id:= TCustomDBGrid(Components[i]).DataSource.DataSet.FieldByName(KeyField).AsString;
end;
Result := id;
end;
Run Code Online (Sandbox Code Playgroud)
ANS(在我的具体案例中) - 在所有人的帮助下:
function getgridId(KeyField: String): string;
var
i: Integer;
id: string;
begin
for i := 0 to Screen.ActiveForm.ComponentCount -1 do
begin
if Screen.ActiveForm.Components[i] is TCustomDBGrid then
id := TCustomDBGrid(Screen.ActiveForm.Components[i]).DataSource.DataSet.FieldByName(KeyField).AsString;
end;
Result := id;
end;
Run Code Online (Sandbox Code Playgroud)
您要求的是要求向函数添加参数以了解要迭代的Form:
function getgridId(Form: TForm; KeyField: String): string;
var
i: Integer;
id: string;
begin
for i := 0 to Form.ComponentCount -1 do
begin
if Form.Components[i] is TCustomDBGrid then
id := TCustomDBGrid(Form.Components[i]).DataSource.DataSet.FieldByName(KeyField).AsString;
end;
Result := id;
end;
Run Code Online (Sandbox Code Playgroud)
然后,当每个Form需要调用该函数时,它可以将它的Self指针作为第一个参数传递.