Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
delphi vcl-如何在标签中设置数据库表的值?_Delphi_Vcl - Fatal编程技术网

delphi vcl-如何在标签中设置数据库表的值?

delphi vcl-如何在标签中设置数据库表的值?,delphi,vcl,Delphi,Vcl,我想在正在创建的框架中查看数据库表的记录,运行时包含显示此数据的标签 我不知道代码是什么 数据看起来是重复的 procedure TForm3.Button1Click(Sender: TObject); var cartRow: TFrm; lin :SmallInt; posX,posY : SmallInt; s , id: string; i : Integer; begin ScrollBox1.DestroyComponents; s := FDQuery

我想在正在创建的框架中查看数据库表的记录,运行时包含显示此数据的标签

我不知道代码是什么 数据看起来是重复的

procedure TForm3.Button1Click(Sender: TObject);
var
  cartRow: TFrm;
  lin :SmallInt;
  posX,posY : SmallInt;
  s , id: string;
  i : Integer;
begin
  ScrollBox1.DestroyComponents;
  s := FDQuery1.FieldByName('CountryAr').AsString;
  id:= FDQuery1.FieldByName('CountryID').AsString;
  posX := 0;
  posY := 0;
  for lin := 0 to FDTable1.RecordCount - 1 do
  begin
    cartRow := TFrm.Create(ScrollBox1);
    cartRow.Parent :=ScrollBox1;
    cartRow.Name := '';
    cartRow.Left := posX -1;
    cartRow.Top := posY -1;
    cartRow.Label1.Caption := (s);
    cartRow.Label2.Caption :=(id);
    cartRow.Width := (ScrollBox1.Width) -3;
    cartRow.Height := 35;
    posY := posY + cartRow.Height +1;
  end;
  cartRow.Free;`

代码中存在多个问题。首先,将值分配给
s
id
一次,然后对每个标签使用相同的值,忽略分配后数据库中的任何内容。第二,在循环中从不推进记录指针,这意味着它将在无限循环中结束。第三,在
FDTable1
字段中循环,但从
FDQuery1
读取值。第四,您不必要地使用了对
RecordCount
的调用,而不是简单的
,而不是Eof
循环。最后,当CartRow不应该免费时,您正在释放它;您将
ScrollBox1
指定为所创建控件的所有者,这意味着当scrollbox空闲时,scrollbox将释放它

这样做对你来说会更好:

procedure TForm3.Button1Click(Sender: TObject);
var
  cartRow: TFrm;
  posX,posY : SmallInt;
begin
  ScrollBox1.DestroyComponents;
  posX := 0;
  posY := 0;
  FDQuery1.First;
  while not FDQuery1.Eof do
  begin
    cartRow := TFrm.Create(ScrollBox1);
    cartRow.Parent := ScrollBox1;
    cartRow.Left := posX - 1;
    cartRow.Top := posY - 1;
    cartRow.Label1.Caption := FDQuery1.FieldByName('CountryAr').AsString;
    cartRow.Label2.Caption := FDQuery1.FieldByName('CountryID').AsString;
    cartRow.Width := ScrollBox1.Width - 3;
    cartRow.Height := 35;
    posY := posY + cartRow.Height + 1;
    FDQuery1.Next;
  end;
end;

下一步,谢谢你,我的朋友,你工作得很好,谢谢你,我全心全意地感谢你