动态生成控件,只要一个窗体,参考代码
constructor TDBEditForm.Create(AOwner:TComponent;SQLString,MasterTabName,
ACaption,DetailTabName,AMasterFields:string;DetailReadOnly:boolean=false);
function CreateDBControl(FADS:TCustomADODataSet;FDataSource:TDataSource;
FOwner:TWinControl;AReadOnly:boolean=false):boolean;
//建立主从表的编辑控件
var
i,j,k,x,y,t,w,p: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;
//取出字段对应的控件类代号,rsTabFields也是一个表,保存编辑字段所用的控件代码,编辑长度,显示等
if rsTabFields.Locate(SField_Name,
FADS.Fields.FieldName,[]) then
begin
t:=rsTabFields.FieldByName(SField_Type).AsInteger;
w:=rsTabFields.FieldByName(SField_EditWidth).AsInteger;
p:=rsTabFields.FieldByName(SField_AccessPopedom).AsInteger;
s:=rsTabFields.FieldByName(SField_Hint).AsString;
end else
begin
t:=0;
w:=100;
p:=2;
s:='';
end;
if iUserGrade>p then
t:=-1;
//根据数据库字段生成相应控件
case t of
0,1: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;
DecimalPlaces := 6;
EditButton.Visible := True;
EditButton.Style := ebsUpDownEh;
end;
2: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;
3:with TDBCheckBoxEh.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;
Flat := true;
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 := iDropDownCount;
DropDownBox.Sizable := True;
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;
end;
//for
end;
begin
screen.Cursor:=crHourGlass;
if length(SqlString)>0 then
try
inherited Create(AOwner);
FMasterTabName := MasterTabName;
FDetailTabName := DetailTabName;
FSql := sqlstring;
FMasterFields := AMasterFields;
Caption := GetCaption(ACaption);
with ADSMaster do
begin
CommandText := Fsql;
CommandType := cmdText;
Open;
end;
if (length(DetailTabName)>0) and (length(FMasterFields)>0) then
begin
//存在从表
with ATBDetail do
begin
if DetailReadOnly then
LockType:=ltReadOnly;
TableName :=DetailTabName;
MasterFields:=FMasterFields;
Open;
end;
MDIMainForm.ProgressStart(0,adsMaster.FieldCount+ATBDetail.FieldCount);
panRight.Width :=Width div 2;
PanRight.Visible:=true;
splRight.Visible:=true;
SetDBGridWidth(DBGMaster{,2});
SetDBGridWidth(DBGDetail{,2});
CreateDBControl(ADSMaster,DSMaster,SlbMaster);
CreateDBControl(ATBDetail,DSDetail,SlbDetail,DetailReadOnly);
end else
begin
//不存在从表
MDIMainForm.ProgressStart(0,adsMaster.FieldCount);
SetDBGridWidth(DBGMaster);
CreateDBControl(ADSMaster,DSMaster,SlbMaster);
end;
except
msgbox(format(SEOpenQueryForm,[caption]),self.text,mb_iconstop);
screen.Cursor:=crDefault;
close;
end;
mdimainform.Progressend;
screen.Cursor:=crDefault;
end;
//===========================================
代码里面一些常量,函数参见
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1425701
里的第一个模块