【问题标题】:Get component text based on component name根据组件名称获取组件文本
【发布时间】:2014-10-07 16:33:25
【问题描述】:

所以我创建了一些像这样的 TEdit 组件

var
  lb : TLabel;
  topLabel, i: Integer;
  dbedit : TEdit;
begin
  inherited;
  topLabel := 40;
  i := 0;
  lb := TLabel.Create(nil);
  lb.Parent := GroupBox2;
  lb.Left := 245;
  lb.Top := 20;
  lb.Caption := 'ASD';
  with  DataModule.myStoredProc do begin
       Close;
       ParamByName('@Param1').AsInteger := 1;
       ExecProc;
       Open;
       SetLength(nrPozitiiDinctionar,RecordCount);
       First;
       while not Eof do begin
           lb := TLabel.Create(nil);
           lb.Parent := GroupBox2;
           lb.Left := 7;
           lb.Top := topLabel  ;
           lb.Caption := FieldByName('X').AsString;
           dbedit := TEdit.Create(nil);
           dbedit.Name := 'Edit'+IntToStr(FieldByName('Poz').AsInteger);
           dbedit.Text := '';
           dbedit.Parent := GroupBox2;
           dbedit.Height := 21;
           dbedit.Width := 40;
           dbedit.Left := 240;
           dbedit.Top := lb.Top-3 ;
           topLabel := topLabel + 30;
           nrPozitiiDinctionar[i] := FieldByName('Poz').AsInteger;
           i := i + 1;
           Next;
       end;

  end;

end;

然后在用户添加他的输入后,我用这段代码运行一个函数

 var
  IDPoz, I : Integer;
  dbedit : TEdit;
  pctj,nume : string;
   begin
      for I := Low(nrPozitiiDinctionar) to High(nrPozitiiDinctionar)  do
         begin
           nume := 'Edit'+IntToStr(nrPozitiiDinctionar[i]);
           pctj := TEdit(FindComponent('Edit'+IntToStr(nrPozitiiDinctionar[i]))).Text;
           with DateCOFurnizori.spCOFCmzFurnizoriEvaluarePozitii_Edit do begin
           ParamByName('@IDEvaluare').AsInteger := StrToInt(Edit1.Text);
           ParamByName('@IDPozitie').AsInteger := IDPoz;
           ParamByName('@DictionarID').AsInteger := 9103;
           ParamByName('@DictionarPozitiiID').AsInteger := nrPozitiiDinctionar[i];
           ParamByName('@Punctaj').AsFloat :=  1 ;//StrToFloat(pctj) ;
           ParamByName('@DataEvaluare').AsDateTime := Now;
           ExecProc;
           IDPoz := IDPoz + 1;
         end;
 end;

这只是代码的一部分,但这应该与我的问题有关。

当我使用调试器时,pctj 中没有任何值,我做错了什么?我尝试根据 TEdits 的名称获取 TEdits 的值。 FindComponent 函数我做错了什么?

【问题讨论】:

  • 您应该在列表中组织控件以避免按名称搜索。只需使用列表的索引即可。
  • 我该怎么做呢?为什么我应该避免按名字搜索?
  • 是的,不要对 FindComponent 大发雷霆。将您的控件放在一个数组中并查找它们。干净得多。
  • 顺便说一句,无需致电ExecProc Open。只需调用一个或另一个。 Open 将执行 SP 并打开返回的数据集(如果有)

标签: delphi components


【解决方案1】:

您没有将Owner 分配给TEdit 控件,这就是FindComponent() 找不到它们的原因。将Self 指定为所有者(因为您正在调用Self.FindComponent()),或者将TEdit 指针存储在TListTObjectList 中,您可以在需要时循环访问。

【讨论】:

  • 那么我应该分配什么所有者?
  • 看文档——我刚刚加了链接
  • FindComponent()TComponent 的方法,因此您必须分配与您调用FindComponent() 相同的组件,在您的示例中可能是您​​的TForm
  • @SirRufo:你抹去了我一半的答案,尤其是我提供替代方案的部分。无论是否错误,您都不应该在发布后立即编辑某人的答案,他们可能仍在编辑它(就像我一样)。回答者有时间先完成。
  • 可以立即编辑答案。如果你想避免有人在你还在写作的时候编辑,不要在写作的时候发帖。如果您想在答案完成一半时发布,就像我们中的许多人一样,那么您将承担风险。
猜你喜欢
  • 2020-11-02
  • 2017-06-22
  • 2021-09-27
  • 1970-01-01
  • 2015-05-25
  • 2015-01-03
  • 1970-01-01
  • 1970-01-01
  • 2014-03-17
相关资源
最近更新 更多