简体   繁体   English

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

[英]delphi vcl - How do I set the values of a database table in a Label?

I want to view the records of the database table in the frame being created and run time contains Label to display this data 我想在正在创建的框架中查看数据库表的记录,并且运行时包含Label来显示此数据

I do not know what the code is The data appears duplicate. 我不知道代码是什么数据似乎重复。

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;`

图片

You have multiple issues in your code. 您的代码中有多个问题。 First, you assign the values to s and id once, and then use those same values for every label, ignoring anything in the database after that assignment. 首先,将值分别分配给sid一次,然后对每个标签使用相同的值,而在分配之后忽略数据库中的任何内容。 Second, you never advance the record pointer in your loop, which means that it will end up in an infinite loop. 其次,您永远不要在循环中前进记录指针,这意味着它将以无限循环结束。 Third, you're looping through FDTable1 fields, but reading the values from FDQuery1 . 第三,您遍历FDTable1字段,但是从FDQuery1读取值。 Fourth, you're unnecessarily using a call to RecordCount instead of a simple while not Eof loop. 第四,您不必要使用对RecordCount的调用,而不是简单的while not Eof循环。 And finally, you're freeing CartRow when it shouldn't be free'd; 最后,当不应该释放CartRow时,您可以释放它。 you're assigning ScrollBox1 as the owner of the control created, which means the scrollbox will free it when the scrollbox is free'd. 您将ScrollBox1分配为创建的控件的所有者,这意味着当滚动框被释放时,滚动框将释放它。

Something like this will work much better for you: 这样的事情将为您更好地工作:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM