控件位置确定(100分)

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

hnzgw

Unregistered / Unconfirmed
GUEST, unregistred user!
最近编个系统时遇到个问题,根据数据库中的记录来生成控件时,控件的位置很难放好,
我的要求是比如有12个记录也即我要生成12对控件,每对是label edit,位置是每一行放
4对像下面一样:
label1 edit1,label2 edit2,label3 edit3,label4 edit4
label5 edit5,label6 edit6,label7 edit7,label8 edit8
label9 edit9,label10 edit10,label11 edit11,label12 edit12
请各位delphi高手帮忙,高手相送!!!!!!!!!!!!!!!
 
高手请进
 
像这样做就可以了。
var
tmpLeft, tmpTop,i, j: integer;//
begin
tmpLeft:= 20;//第一个控件的左部位置,你可修改
tmpTop:= 20;//第一个控件的顶部位置,你可修改
i:= 0;
while i<12 do
begin
j:= 0;
while j<8 do
begin
if odd(j) then
begin
生成一个Label控件;
Labe.Left:= tmpLeft;
Label.Top:= tmpTop;
tmpLeft:= tmpLeft + Label.Width;
end
else
begin
生成一个Edit控件;
Edit.Left:= tmpLeft;
Edit.Top:= tmpTop;
tmpLeft:= tmpLeft + Edit.Width;
end;
j:= j + 1;
end;
tmpLeft:=20;//准备生成下一行控件
tmpTop:= tmpTop+30;//30为行间距,你可修改
j:= 0;
i:= i + 1;
end;

 
to:djrj
再次关注:
如果有11个记录

label1 edit1,label2 edit2,label3 edit3,label4 edit4
label5 edit5,label6 edit6,label7 edit7,label8 edit8
label9 edit9,label10 edit10,label11 edit11
这样怎么办

 
给你参考一段代码,我就是这样做的,产生后的是系统默认位置,在界面上还可
以由用户拖动
CREATE proc DPosDefalt

as
--控件默认位置计算
--用户每增加一个字段,都要调用此方法来确定此控件的位置,并要增加一个相应的标签
--取得最后一个控件的位置,并看是否处在最后一个位置上,
--如是:标签:高=高+24,左=8,宽=20--文本框--高=高+24,左=28,宽=150
--不是:标签:高=高,左=左+2,宽=20--文本框--高=高,左=左+2,宽=150
--取得最后一行
declare @top int
declare @left int
declare @width int
declare @lst bit
declare @lst1 bit
declare @id int
declare @name varchar(20)
declare @oldname varchar(20)
--增加一个标签
select @top=0,@left=0,@width=0,@lst=0
select top 1
@top=isnull(高,0),
@left=isnull(左,0),
@width=isnull(宽,0),
@lst=isnull(lst,0),
@name=isnull(名称,'')
from ditempos order by id desc
if @name is null
begin
select top 1 @name=名称 from ditemdefine order by id
end
if @top=0 set @top=24
if @left=0 set @left=8
set @oldname=@name
if @lst=1--最后一个标签已处在最后一个位置
begin
set @name=@name+'Lb'
if @left+8>=488
set @lst1=1
else
set @lst1=0
select @top=@top+24,
@left=8,
@width=20
insert ditempos(名称,高,左,宽,lst,类型)
values(@name,@top,@left,@width,0,'Label')

end
else begin--最后一个标签已不在最后一个位置
set @name=@name+'Lb'
if @left+@width+2>=488
set @lst1=1
else
set @lst1=0
select @top=@top,
@left=@left+@width+2,
@width=20
insert ditempos(名称,高,左,宽,lst,类型)
values(@name,@top,@left,@width,@lst1,'Label')
end
select @top=0,@left=0,@width=0,@lst=0
--增加一个文本框
select top 1
@top=isnull(高,0),
@left=isnull(左,0),
@width=isnull(宽,0),
@lst=lst
from ditempos order by id desc
if @top=0 set @top=24
if @left=0 set @left=8
if @lst=1--已处在最后一个位置
begin
if @left+8>=488
set @lst1=1
else
set @lst1=0
select @top=@top+24,
@left=8,
@width=150
insert ditempos(名称,高,左,宽,lst,类型)
values(@oldname,@top,@left,@width,0,'DBEdit')
end
else begin--不在最后一个位置
if @left+8>=488
set @lst1=1
else
set @lst1=0
select @top=@top,
@left=@left+2+@width,
@width=150
insert ditempos(名称,高,左,宽,lst,类型)
values(@Oldname,@top,@left,@width,@lst1,'DBEdit')
end



GO
 
//生成的控件放在ScollBox中,就不用担心用户找不到了
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;

//取出字段对应的控件类代号
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;
{ 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;}
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
with TLabel.Create(self) do
begin
Parent := FOwner;
Alignment := taLeftJustify;
AutoSize := false;
left := x+LABLE_MAX_WIDTH;
top := y+3;
Width := LABLE_MAX_WIDTH;
Caption := SNoPopedomViewField;
end;
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;
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;
 
to:hnzgw
若你有11个记录,或记录数不确定时,
你只要在程序中的设个变量count,
count:=Table1.RecordCount;
再把程序中的常量12换成这个变量不就解决了。
按照这种思路,再设个变量作为字段数,
这样,你不但可以动态生成每行的控件数,
也可以动态生成总的控件的行数,
 
我试试再说
 
我发邮件给你了,收到没有
 
接受答案了.
 

Similar threads

后退
顶部