//刚写完。 Procedure MyCreateLbl; var lbl:tlabel; begin with lbl do begin tlabel.create(); left:=20; top:=20; width:=40; height:=10; caption:='你的显示值'; Parent:=Panel2;//设置在哪个控件上显示 Visable:= true; end; end;
TO OUTER2000:根据你说的用LISTVIEW是可以实现目标,但有这样一个问题,LISTVIW只能一行显示一个员工的姓名,当一个部门底下有很多员工时,界面就不美观,同时会造成界面空间的浪费。
首先:ListView的Columns决定了一行显示多少列其次 要是用Label的话 var lbl:array of TLabel; i,iTop,ileft:integer;
qry.sql.text:='select EmployeeName from yourtable';SetLength(lbl,qry.RecordCount) i:=0; while not qry.eof do begin lbl[i]:=TLabel.Create(self); lbl[i].Parent:=self; iLeft:=(i mod 5)*80+10; iTop:=((i mod 5)=0)*i+10; lbl[i].Left:=iLeft; lbl[i].Top;=iTop; lbl[i].Caption:=qry['EmployeeName']; inc(i); qry.next; end;
to 西周生:谢谢你的指点,我有用LBABEL实现了该功能,但效果还是不理想,所以我想改成Listview控件,请问具体要怎么编程?烦请再指点。 以下是我写的一点代码。 procedure tform1.dbclick(Sender: TObject); begin showmessage('测试'); end; procedure TForm1.TreeView2Click(Sender: TObject); const x_increament=100;//员工姓名的列间隔 y_increament=80;//员工姓名的行间隔 x_init=0; //初始横坐标 y_init=10; //初始纵坐标 col_count=5; //每行要显示的员工姓名个数 var row_count:integer; //要显示的员工姓名的行数 vlabel:tlabel; i:integer; //循环变量 begin TVData := TreeView2.Selected.Data; if TVData <> nil then begin glb_bmdm := TVData.indexID; end; adoquery1.Close; adoquery1.Parameters.ParamByName('bmdm').Value:=glb_bmdm; //bmdm表示部门代码 adoquery1.Open; row_count:=0; if adoquery1.RecordCount > 0 then begin
adoquery1.First; while not adoquery1.Eof do begin for i:=1 to col_count do begin if not adoquery1.eof then begin vlabel:=TLabel.Create(Self); with vlabel do begin vlabel:=tlabel.create(self); Parent:=panel2; left:=x_init+i*x_increament; top:=y_init+row_count * y_increament; width:=150; height:=30; autosize:=false; caption:=adoquery1.fieldbyname('xm').asstring;//xm表示姓名 ondblclick:=dbclick; cursor:=crHandpoint; show; end; end else exit; adoquery1.next; end; if not adoquery1.eof then row_count:=row_count+1; end; end; end;
才上来看到你的短消息,这种情况最好采用ListView来做
没有检查 没有优化,仅供参考var i,j:integer; ListItem:array of TListItem; begin //²éѯ³öÄãÒªµÃµÄÔ±¹¤Ãû³ÆÁбí adoquery1.First; setLength(ListItem,(adoquery1.RecordCount div 5)); i:=0; j:=0; while not adoquery1.Eof do begin if boolean(i mod 5) then begin ListItem[j]:=TListItem.Create(listview1.Items); listview1.Items.AddItem(ListItem[j],adoquery1['Ô±¹¤Ãû³Æ']); inc(j); end else listview1.Items.Item[j].SubItems.Add(adoquery1['Ô±¹¤Ãû³Æ']); adoquery1.Next; end; end;
var i,j:integer; ListItem:array of TListItem; begin
//查询员工名称 adoquery1.First; setLength(ListItem,(adoquery1.RecordCount div 5)); i:=0; j:=0; while not adoquery1.Eof do begin if boolean(i mod 5) then begin ListItem[j]:=TListItem.Create(listview1.Items); listview1.Items.AddItem(ListItem[j],adoquery1['员工名称']); inc(j); end else listview1.Items.Item[j].SubItems.Add(adoquery1['员工名称']); adoquery1.Next; end; end;
l:TLabel;
begin
l:=TLabel.Create(self);
l.Name:='Label1';//...
l.Owner:=Panel2;
l.top:=11;
l.left:=12;
...
end;
l1.left:=20;
l1.top:=20;
l1.width:=40;
l1.height:=10;
l1.caption:='你的显示值';//照这个例子写吧
Procedure MyCreateLbl;
var
lbl:tlabel;
begin
with lbl do
begin
tlabel.create();
left:=20;
top:=20;
width:=40;
height:=10;
caption:='你的显示值';
Parent:=Panel2;//设置在哪个控件上显示
Visable:= true;
end;
end;
var
lbl:array of TLabel;
i,iTop,ileft:integer;
qry.sql.text:='select EmployeeName from yourtable';SetLength(lbl,qry.RecordCount)
i:=0;
while not qry.eof do
begin
lbl[i]:=TLabel.Create(self);
lbl[i].Parent:=self; iLeft:=(i mod 5)*80+10;
iTop:=((i mod 5)=0)*i+10; lbl[i].Left:=iLeft;
lbl[i].Top;=iTop;
lbl[i].Caption:=qry['EmployeeName'];
inc(i);
qry.next;
end;
以下是我写的一点代码。
procedure tform1.dbclick(Sender: TObject);
begin
showmessage('测试');
end;
procedure TForm1.TreeView2Click(Sender: TObject);
const
x_increament=100;//员工姓名的列间隔
y_increament=80;//员工姓名的行间隔
x_init=0; //初始横坐标
y_init=10; //初始纵坐标
col_count=5; //每行要显示的员工姓名个数
var
row_count:integer; //要显示的员工姓名的行数
vlabel:tlabel;
i:integer; //循环变量
begin
TVData := TreeView2.Selected.Data;
if TVData <> nil then
begin
glb_bmdm := TVData.indexID;
end;
adoquery1.Close;
adoquery1.Parameters.ParamByName('bmdm').Value:=glb_bmdm; //bmdm表示部门代码
adoquery1.Open;
row_count:=0;
if adoquery1.RecordCount > 0 then
begin
adoquery1.First; while not adoquery1.Eof do
begin
for i:=1 to col_count do
begin
if not adoquery1.eof then
begin
vlabel:=TLabel.Create(Self);
with vlabel do
begin
vlabel:=tlabel.create(self);
Parent:=panel2;
left:=x_init+i*x_increament;
top:=y_init+row_count * y_increament;
width:=150;
height:=30;
autosize:=false;
caption:=adoquery1.fieldbyname('xm').asstring;//xm表示姓名
ondblclick:=dbclick;
cursor:=crHandpoint;
show;
end;
end
else
exit;
adoquery1.next;
end;
if not adoquery1.eof then
row_count:=row_count+1;
end; end;
end;
i,j:integer;
ListItem:array of TListItem;
begin
//²éѯ³öÄãÒªµÃµÄÔ±¹¤Ãû³ÆÁбí
adoquery1.First;
setLength(ListItem,(adoquery1.RecordCount div 5));
i:=0;
j:=0;
while not adoquery1.Eof do
begin
if boolean(i mod 5) then
begin
ListItem[j]:=TListItem.Create(listview1.Items);
listview1.Items.AddItem(ListItem[j],adoquery1['Ô±¹¤Ãû³Æ']);
inc(j);
end
else
listview1.Items.Item[j].SubItems.Add(adoquery1['Ô±¹¤Ãû³Æ']); adoquery1.Next;
end;
end;
i,j:integer;
ListItem:array of TListItem;
begin
//查询员工名称 adoquery1.First;
setLength(ListItem,(adoquery1.RecordCount div 5));
i:=0;
j:=0;
while not adoquery1.Eof do
begin
if boolean(i mod 5) then
begin
ListItem[j]:=TListItem.Create(listview1.Items);
listview1.Items.AddItem(ListItem[j],adoquery1['员工名称']);
inc(j);
end
else
listview1.Items.Item[j].SubItems.Add(adoquery1['员工名称']); adoquery1.Next;
end;
end;