//贴源代码给你参考
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;