{"id":4276,"date":"2014-03-30T09:38:29","date_gmt":"2014-03-30T09:38:29","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/calling-tedit-objects-based-on-db-query-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:38:29","modified_gmt":"2014-03-30T09:38:29","slug":"calling-tedit-objects-based-on-db-query-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/calling-tedit-objects-based-on-db-query-collection-of-common-programming-errors\/","title":{"rendered":"Calling TEdit objects based on DB query-Collection of common programming errors"},"content":{"rendered":"<p>Use FindComponent to &#8220;convert&#8221; a component name to the component itself:<\/p>\n<pre><code>var\n  Edit: TEdit;\n  I: Integer;\nbegin\n  DataSet.First;\n  I := 1;\n  while not DataSet.Eof do\n  begin\n    Edit := TEdit(FindComponent(Format('EditPhone%d', [I])));\n    if Edit  nil then\n      Edit.Text := DataSet.FieldValues['PhoneNo'];\n    DataSet.Next;\n    Inc(I);\n  end;\n<\/code><\/pre>\n<p>Now, this requires to hard-code the <code>EditPhone%d<\/code> string into the source which results in all kinds of maintainability issues. For example: consider renaming the edits.<\/p>\n<h3>Alternative 1:<\/h3>\n<p>To not rely on the component names, you could instead make use of TLama&#8217;s idea and add all the edits to a list:<\/p>\n<pre><code>uses\n  ... , Generics.Collections;\n\ntype\n  TForm1 = class(TForm)\n    EditPhone1: TEdit;\n    ...\n    procedure FormCreate(Sender: TObject);\n    procedure FormDestroy(Sender: TObject);\n  private\n    FEdits: TList;\n  end;\n\nprocedure TForm1.FormCreate(Sender: TObject);\nbegin\n  FEdits := TList.Create;\n  FEdits.AddRange([EditPhone1, EditPhone2, EditPhone3, EditPhone4, EditPhone5,\n    EditPhone6, EditPhone7]);\nend;\n\nprocedure TForm1.FormDestroy(Sender: TObject);\nbegin\n  FEdits.Free;\nend;\n\nprocedure TForm1.ADOQuery1AfterOpen(DataSet: TDataSet);\nvar\n  I: Integer;\nbegin\n  DataSet.First;\n  I := 0;\n  while (not DataSet.Eof) and (I &lt; FEdits.Count) do\n  begin\n    FEdits[I].Text := DataSet.FieldValues['PhoneNo'];\n    DataSet.Next;\n    Inc(I);\n  end;\nend;\n<\/code><\/pre>\n<p>This still requires some maintenance in case of adding edits in future.<\/p>\n<h3>Alternative 2:<\/h3>\n<p>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:<\/p>\n<pre><code>procedure TForm1.FormCreate(Sender: TObject);\nvar\n  I: Integer;\nbegin\n  FEdits := TList.Create;\n  for I := 0 to ComponentCount - 1 do\n    if (Components[I] is TEdit) and (TEdit(Components[I]).Tag = 1) then\n      FEdits.Add(TEdit(Components[I]));\nend;\n<\/code><\/pre>\n<p>But keeping those tags up to date is another burden.<\/p>\n<h3>Alternative 3:<\/h3>\n<p>I suggest you use a <code>TDBGrid<\/code> 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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Use FindComponent to &#8220;convert&#8221; a component name to the component itself: var Edit: TEdit; I: Integer; begin DataSet.First; I := 1; while not DataSet.Eof do begin Edit := TEdit(FindComponent(Format(&#8216;EditPhone%d&#8217;, [I]))); if Edit nil then Edit.Text := DataSet.FieldValues[&#8216;PhoneNo&#8217;]; DataSet.Next; Inc(I); end; Now, this requires to hard-code the EditPhone%d string into the source which results in all [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-4276","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4276","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=4276"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4276\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4276"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4276"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4276"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}