帮我看看这段代码 (100分)

  • 主题发起人 主题发起人 huayf
  • 开始时间 开始时间
H

huayf

Unregistered / Unconfirmed
GUEST, unregistred user!
var
S: string ; //--保存从表中读出的值
text:string;
lbl:^tlabel;
begin
text:=dbcombobox1.Text ;

//-------------------读问题列表的值---------------
while not adoquery1.Eof do
begin
with adoquery1 do
begin
close;
sql.Clear ;
sql.Text:= 'select ques from quest where name='''+text+'''';
open;
end;
s:=adoquery1.fieldbyname('ques').Value ;
showmessage(s);

//----------------动态生成控件-----------------
lbl:=allocmem(sizeof(Tlabel));
lbl^:=Tlabel.create(self);
with lbl^ do
begin
parent:=controlbar1;
caption:=s;
font.Name :='楷体-gb2312';
font.Size :=11;
// font.Style.fsbold;
end;

//-----------------读下一条记录-----------------
adoquery1.Next ;
end;

end;
我要的是这样的结果:即从一个表中一条一条读出某个符合条件的值,读出一条记录动态生成一个控件,直到把所有符合条件的值读出并生成相应的控件,控件数是不确定的,视符合条件的记录数而定。先谢过!
 
var
S: string ; //--保存从表中读出的值
text:string;
//lbl:^tlabel;
begin
text:=dbcombobox1.Text ;

//-------------------读问题列表的值---------------
with adoquery1 do
begin
close;
sql.Clear ;
sql.Text:= 'select ques from quest where name='''+text+'''';
open;
end;

while not adoquery1.Eof do
begin
s:=adoquery1.fieldbyname('ques').Value ;
showmessage(s);

//----------------动态生成控件-----------------
//lbl:=allocmem(sizeof(Tlabel));
//lbl^:=Tlabel.create(self);
with Tlabel.create(controlbar1) do
begin
//parent:=controlbar1;
caption:=s;
font.Name :='楷体-gb2312';
font.Size :=11;
// font.Style.fsbold;
end; }

//-----------------读下一条记录-----------------
adoquery1.Next ;
end;

end;
 
//贴源代码给你参考
function CreateDBControl(FADS:TCustomADODataSet;FDataSource:TDataSource;
FOwner:TWinControl;AReadOnly:boolean=false):boolean;
//建立主从表的编辑控件
var
i,j,k,x,y,t,w:integer;
s:string;
pm:TPopupMenu;
mi:TMenuItem;
begin
j:=0;x:=-20;y:=7;
result:=true;
for i:=0 to FADS.Fields.Count-1 do
begin
//生成标签
with TLabel.Create(self) do
begin
Parent := FOwner;
Alignment := tarightjustify;
AutoSize := false;
left := x;
top := y+3;
Width := LABLE_MAX_WIDTH;
Caption := FADS.Fields.FieldName+':';
end;

//取出字段对应的控件类代号
if rsTabFields.Locate(SField_Name,
FADS.Fields.FieldName,[]) then
begin
t:=rsTabFields.FieldByName(SField_Type).AsInteger;
w:=rsTabFields.FieldByName(SField_EditWidth).AsInteger;
s:=rsTabFields.FieldByName(SField_Hint).AsString ;
end else begin
t:=0;
w:=100;
s:='';
end;
//根据数据库字段生成相应控件
case t of
0:with TDBNumberEditEh.create(FOwner) do //数字编辑框
begin
Parent := FOwner;
AutoSize := false;
AutoSelect := true;
Left := x+LABLE_MAX_WIDTH;
Top := y;
Width := w;
Height := EDIT_HEIGHT;
DataSource := FDataSource;
DataField := FADS.Fields.FieldName;
Hint := s;
Flat := true;
ReadOnly := AReadOnly;
end;
1:with TDBEditEh.create(FOwner) do //普通编辑框
begin
Parent := FOwner;
AutoSize := false;
AutoSelect := true;
Left := x+LABLE_MAX_WIDTH;
Top := y;
Width := w;
Height := EDIT_HEIGHT;
DataSource := FDataSource;
DataField := FADS.Fields.FieldName;
Hint := s;
Flat := true;
ReadOnly := AReadOnly;
end;
2:with TDBMemo.Create(FOwner) do //多行编辑框
begin
Parent := FOwner;
AutoSize := false;
Left := x+LABLE_MAX_WIDTH;
Top := y;
Width := w;
Height := FieldMaxHeight*2-5;
DataSource := FDataSource;
DataField := FADS.Fields.FieldName;
Hint := s;
ReadOnly := AReadOnly;
end;
2,3:with TDBCheckBox.create(FOwner) do //复选框
begin
Parent := FOwner;
Left := x+LABLE_MAX_WIDTH;
Top := y+1;
Width := w;
Height := EDIT_HEIGHT;
Caption := FADS.Fields.FieldName;
DataSource := FDataSource;
DataField := FADS.Fields.FieldName;
Hint := s;
ReadOnly := AReadOnly;
end;
4:with TDBDateTimeEditEh.create(FOwner) do //日期编辑框
begin
Parent := FOwner;
AutoSize := false;
AutoSelect := true;
Left := x+LABLE_MAX_WIDTH;
Top := y;
Width := w;
Height := EDIT_HEIGHT;
DataSource := FDataSource;
DataField := FADS.Fields.FieldName;
Hint := s;
Flat := true;
ReadOnly := AReadOnly;
end;
5..7:with TDBComboBoxEh.Create(FOwner) do //下拉列表框
begin
Parent := FOwner;
Left := x+LABLE_MAX_WIDTH;
Top := y;
Width := w;
Height := EDIT_HEIGHT;
DataSource := FDataSource;
DataField := FADS.Fields.FieldName;
Hint := s;
Flat := true;
ReadOnly := AReadOnly;
DropDownBox.Rows:=iMaxDropDownCount;
if t<>6 then Items:=GetRecordSetStrings(
rsTabFields.FieldByName(SField_DataSource).AsString);
if t=7 then
begin
pm:=TPopupMenu.Create(self);
for k:=0 to items.Count-1 do
begin
mi := TMenuItem.Create(self);
mi.Caption := items[k];
mi.Hint := items[k];
mi.Tag := i;
mi.OnClick := pmTempClick;
pm.Items.Add(mi);
end;
EditButtons.Add;
EditButtons[0].Style := ebsPlusEh;
EditButtons[0].DropdownMenu := pm;
end;
if t=6 then
OnEnter:=FieldEnter;
end;
else continue;
end;//case

//控件记数器,调整控件位置
j := j+1;
y := y+FIELD_MAX_HEIGHT;
if j=FIELD_MAX_LINES then
begin
j := 0;
x := x+FIELD_MAX_WIDTH;
y := 7;
end;
mdimainform.ProgressAdd;
end;//for
end;
 
lb1: TLabel;
lb1本身就是一个指针,4个字节,只是没有把它叫做指针而已
不需要定义^TLabel;
TLabel.Create函数本身就会分配内存,你也就不许要在开辟内存
所以
lb1: TLabel;
lb1:=TLabel.Create(Self)就可以了
lb1.parent:=...
 
我自己解决了。
 
都解决了,我也没有回答的必要了!散分
 
后退
顶部