怎样动态的添加子项,还有,怎么同时选定一行

解决方案 »

  1.   

    引用CommCtrl单元procedure TForm1.Button1Click(Sender: TObject);begin   ListView_DeleteColumn(MyListView.Handle, i);//i是要删除的列的序号,从0开始end;用LISTVIEW显示表中的信息:
    procedure viewchange(listv:tlistview;table:tcustomadodataset;var i:integer);
     begin
           tlistview(listv).Items.BeginUpdate;   {listv:listview名}
            try
              tlistview(listv).Items.Clear;
              with table do         {table or query名}
               begin
                active:=true;
                first;
                while not eof do
                 begin
                  listitem:=tlistview(listv).Items.add;
                  listitem.Caption:=trim(table.fields[i].asstring);
      //            listitem.ImageIndex:=8;
                 next;
                 end;
            end;
            finally
            tlistview(listv).Items.EndUpdate;
          end;
     end;
      

  2.   

    procedure GetDirectories(list: TlistView; Directory: string; Item: Tlistitem; IncludeFiles: boolean);
    var
    SearchRec: TSearchRec;
    begin
       list.Items.BeginUpdate;//准备更新
       list.Items.Clear;  //清空list内容
       if Directory[length(Directory)] <> '\' then //判断路径 
             Directory := Directory + '\';  //设置路径
       if FindFirst(Directory + '*.*',faDirectory,SearchRec) = 0 then       begin
                 repeat    
                 if (SearchRec.Attr and faDirectory = faDirectory) and (SearchRec.Name[1] <> '.') then  
                     begin
                            if (SearchRec.Attr and faDirectory > 0) then                               begin  Item := list.Items.Add;  //增加item
                                            item.Caption:=SearchRec.Name; 
                                            item.ImageIndex:=6;
                                  end;
                            GetDirectories(list,Directory + SearchRec.Name,Item,IncludeFiles);
                     end
                                   else
                   if IncludeFiles then
                                if SearchRec.Name[1] <> '.' then
                                        begin
                                               item:=list.Items.Add;
                                               item.Caption:=SearchRec.Name;
                                               item.ImageIndex:=6;
                                        end;
                          until FindNext(SearchRec) <> 0;
    //FindClose(SearchRec);
                   end;
          list.Items.EndUpdate;
    end;定义Directory:procedure makedir(modulname,pathname,dirname,filepath:string);//创建目录(共4级,朋友如果觉得用得上的话,可以自己加参数)
    begin
    try
        if not directoryexists(frootpath+modulname) then
          createdir(FRootPath+modulname);
        if not DirectoryExists(FRootPath+modulname+'\'+pathname) then
        createdir(FRootPath+modulname+'\'+pathname);
        if not directoryexists(FRootPath+modulname+'\'+pathname+'\'+dirname) then
        createdir(FRootPath+modulname+'\'+pathname+'\'+dirname);
        if filepath<>'' then
        if not directoryexists(FRootPath+modulname+'\'+pathname+'\'+dirname+'\'+filepath) then
        createdir(FRootPath+modulname+'\'+pathname+'\'+dirname+'\'+filepath)
        except
        On E:Exception do begin
        abort;
        end;
        end;
        banjin.N21.Enabled:=true;
       liucheng.MenuItem1.Enabled:=true;
       yanzheng.N21.Enabled:=true;
        banjin.SpeedButton1.Enabled:=true;
        liucheng.SpeedButton1.Enabled:=true;
        yanzheng.SpeedButton1.Enabled:=true;
        banjin.toolbutton1.Enabled:=true;
        liucheng.toolbutton1.Enabled:=true;
        yanzheng.toolbutton1.Enabled:=true;
        if filepath='' then
        filespath:=FRootPath+modulname+'\'+pathname+'\'+dirname+'\'+filepath else
        filespath:=FRootPath+modulname+'\'+pathname+'\'+dirname+'\'+filepath+'\';
        banjin.StatusBar1.Panels[0].Text:='你目前所在的位置:'+filespath;
        liucheng.StatusBar1.Panels[0].Text:='你目前所在的位置:'+filespath;
        yanzheng.StatusBar1.Panels[0].Text:='你目前所在的位置:'+filespath;
    end;
    如果是二级或一级目录的话,只要加上if filepath<>'' then
    类似的判断就可以了,例如:makedir('设计规范','电子件','电路设计规范',''); 注:在这里写的两个过程没有实现读取windows的图标,如果想实现这一功能的话要自己动手写了 ^_^
      

  3.   

    下列程序是本人在软件开发过程中根据用户操作上的方便,写的一个小过程,主要实现了向listview控件中拖放文件功能,其源代码如下:procedure tyanzheng.AppMessage(var Msg: TMsg;var Handled: Boolean); 
    var
    nFiles, I: Integer;
    ListItem: TListItem;
    begin
    if (Msg.message = WM_DROPFILES) and (msg.hwnd = ListView1.Handle) then
    begin
    if MessageDlg('确定要加入吗',
        mtConfirmation, [mbYes, mbNo], 0) = mrYes then begin
    // 取dropped files的数量
    nFiles := DragQueryFile (Msg.wParam, $FFFFFFFF, nil, 0);
    // 循环取每个拖下文件的全文件名
    try
    for I := 0 to nFiles - 1 do
    begin
    // 为文件名分配缓冲 allocate memory
    SetLength (Filename, 80);
    // 取文件名 read the file name
    DragQueryFile (Msg.wParam, I, PChar (Filename), 80);
    Filename := PChar (Filename);
    //将全文件名分解程文件名和路径
    ListItem := ListView1.Items.Add;
    ListItem.Caption := ExtractFileName(FileName);
    listitem.ImageIndex:=6;
    ListItem.SubItems.Add(ExtractFilePath(FileName));
    filepath:=extractfilepath(filename);
    //drage:=true;
    end;
    finally
    //结束这次拖放操作
    DragFinish (Msg.wParam);
    end;
    //标识已处理了这条消息
    Handled := True;
     movefile(pchar(filename),pchar(filespath+listitem.caption));
    end;end;
    end;
    注意:本程序功能是完整的,程序中使用了movefile函数,也就是说,实现了把操作的文件移到了系统所在的目录中,因此建议在调试过程中不要用重要的文件作调试用。程序中用到了向windows发送消息,不明白的话,建议看一些有关方面的文章(本程序可直接运行,须加入到系统中),本程序与《tlistview显示文件夹内容中的程序结合使用,效果更好》程序用在showform事件中:用法如下://设置需要处理文件WM_DROPFILES拖放消息
    DragAcceptFiles(ListView1.Handle, TRUE);
    //设置AppMessage过程来捕获所有消息
    Application.OnMessage := AppMessage;谢谢支持
      

  4.   

    给你个简单的例子:
           with ClientDataSetSelect do
           while not eof do
           begin
               tempListItem:=ListViewSelect.Items.Add;
               tempListItem.Caption:=FieldByName(szFldID).asstring;
               tempListItem.SubItems.Add(FieldByName(szFLdName).asstring);
               tempListItem.SubItems.Add(FieldByName(szFldkjm).asstring);
               next;
           end;
         endClientDataSetSelect可以是QUERY
      

  5.   

    procedure TForm1.Button1Click(Sender: TObject);begin   ListView_DeleteColumn(MyListView.Handle, i);//i是要删除的列的序号,从0开始end;用LISTVIEW显示表中的信息:
    procedure viewchange(listv:tlistview;table:tcustomadodataset;var i:integer);
     begin
           tlistview(listv).Items.BeginUpdate;   {listv:listview名}
            try
              tlistview(listv).Items.Clear;
              with table do         {table or query名}
               begin
                active:=true;
                first;
                while not eof do
                 begin
                  listitem:=tlistview(listv).Items.add;
                  listitem.Caption:=trim(table.fields[i].asstring);
      //            listitem.ImageIndex:=8;
                 next;
                 end;
            end;
            finally
            tlistview(listv).Items.EndUpdate;
          end;
     end;