在一个FORM上有左右两个panel1,panel2,中间用Splitter1隔开,在左边的panel1上有一个treeview1,它可以动态的从数据库中读出部门代码,并生成树型图,当点击子部门时,该子部门的所有员工的姓名都要显示在右边的panel2上,当双击姓名时,可以弹出一个窗口显示该员工的基本信息。我如果要动态创建Label的方式来显示员工的姓名,最好是每行显示5个人的姓名,请问该如何编程。谢谢!

解决方案 »

  1.   

    var
      l:TLabel;
    begin
      l:=TLabel.Create(self);
      l.Name:='Label1';//...
      l.Owner:=Panel2;
      l.top:=11;
      l.left:=12;
      ...
    end;
      

  2.   

    l1:tlabel;l1:=tlabel.create();
    l1.left:=20;
    l1.top:=20;
    l1.width:=40;
    l1.height:=10;
    l1.caption:='你的显示值';//照这个例子写吧
      

  3.   

    //刚写完。
    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;  
      

  4.   

    员工的姓名也是从数据库中,所以我们根本就不知道一个部门底下有多少个员工,所以在程序中给它设置left,top值就不太合理了
      

  5.   

    这情况一般采用LISTVIW来显示员工的,直接就可在右边显示员工信息;然后点某个员工,弹出窗口进行编辑;
      

  6.   

    TO OUTER2000:根据你说的用LISTVIEW是可以实现目标,但有这样一个问题,LISTVIW只能一行显示一个员工的姓名,当一个部门底下有很多员工时,界面就不美观,同时会造成界面空间的浪费。
      

  7.   

    首先: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;
      

  8.   

    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;
      

  9.   

    才上来看到你的短消息,这种情况最好采用ListView来做
      

  10.   

    没有检查 没有优化,仅供参考var
       i,j:integer;
       ListItem:array of TListItem;
    begin
      //&sup2;é&Ntilde;&macr;&sup3;&ouml;&Auml;&atilde;&Ograve;&ordf;&micro;&Atilde;&micro;&Auml;&Ocirc;±&sup1;¤&Atilde;&ucirc;&sup3;&AElig;&Aacute;&ETH;±í
      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['&Ocirc;±&sup1;¤&Atilde;&ucirc;&sup3;&AElig;']);
          inc(j);
        end
        else
          listview1.Items.Item[j].SubItems.Add(adoquery1['&Ocirc;±&sup1;¤&Atilde;&ucirc;&sup3;&AElig;']);    adoquery1.Next;
      end;
    end;
      

  11.   

    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;
      

  12.   

    应该是 if  boolean((i mod 5)+1) then:-)  写的太快了
      

  13.   

    listview1可以通过更改column属性来显示多少列