根据数据库查询调用TEdit对象 [英] Calling TEdit objects based on DB query

查看:65
本文介绍了根据数据库查询调用TEdit对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有7 TEdit的表单,其名称为EditPhone1,EditPhone2,依此类推。
在同一表格中,我查询数据库以获取填充这些TEdit的数据。当然,我无法事先知道查询将返回多少结果。
在循环查询行数时如何调用各种TEdit对象?

I have a form with 7 TEdit having name EditPhone1, EditPhone2 and so on. In the same form I query a DB to get data to fill those TEdits. Of course I cannot know in advance how many results the query will return. How can I call the various TEdit objects when looping on the rowcount of the query?

推荐答案

使用 FindComponent 来转换。

var
  Edit: TEdit;
  I: Integer;
begin
  DataSet.First;
  I := 1;
  while not DataSet.Eof do
  begin
    Edit := TEdit(FindComponent(Format('EditPhone%d', [I])));
    if Edit <> nil then
      Edit.Text := DataSet.FieldValues['PhoneNo'];
    DataSet.Next;
    Inc(I);
  end;

现在,这需要对 EditPhone%d 进行硬编码串入源代码,这会导致各种可维护性问题。例如:考虑重命名编辑。

Now, this requires to hard-code the EditPhone%d string into the source which results in all kinds of maintainability issues. For example: consider renaming the edits.

要不依赖组件名称,可以改用 TLama的想法并将所有修改添加到列表中:

To not rely on the component names, you could instead make use of TLama's idea and add all the edits to a list:

uses
  ... , Generics.Collections;

type
  TForm1 = class(TForm)
    EditPhone1: TEdit;
    ...
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FEdits: TList<TEdit>;
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FEdits := TList<TEdit>.Create;
  FEdits.AddRange([EditPhone1, EditPhone2, EditPhone3, EditPhone4, EditPhone5,
    EditPhone6, EditPhone7]);
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FEdits.Free;
end;

procedure TForm1.ADOQuery1AfterOpen(DataSet: TDataSet);
var
  I: Integer;
begin
  DataSet.First;
  I := 0;
  while (not DataSet.Eof) and (I < FEdits.Count) do
  begin
    FEdits[I].Text := DataSet.FieldValues['PhoneNo'];
    DataSet.Next;
    Inc(I);
  end;
end;

在将来添加编辑内容时,这仍然需要一些维护。

This still requires some maintenance in case of adding edits in future.

您还可以遍历表单中的所有编辑,以找到标记为要添加到列表中的那些编辑,而不是显式地添加它们:

You could also loop over all edits in the form to find the ones tagged to be added to the list, instead of adding them each explicitly:

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  FEdits := TList<TEdit>.Create;
  for I := 0 to ComponentCount - 1 do
    if (Components[I] is TEdit) and (TEdit(Components[I]).Tag = 1) then
      FEdits.Add(TEdit(Components[I]));
end;

但保持这些标签为最新状态是另一个负担。

But keeping those tags up to date is another burden.

我建议您使用 TDBGrid 这是一个数据组件。打开链接的数据集将自动将所有电话号码添加到网格中。通过某些设置,网格可能看起来像是在彼此下方进行了几次编辑。

I suggest you use a TDBGrid which is a data-component. Opening the linked dataset will automatically add all phone numbers to the grid. With some settings, the grid may kind of look like a couple of edits below each other.

这篇关于根据数据库查询调用TEdit对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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