您的位置:程序门 -> vb -> 基础类



如何正确显示树型结构?


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


如何正确显示树型结构?
发表于:2008-01-09 14:02:52 楼主
数据库里的表如下:  
                                            科目编码                           名称                                   上级科目编码  
                                                        a1                                           aa                                               r  
                                                a2                                           bb                                               r  
                                                a3                                           cc                                               a1  
                                                a4                                           dd                                               a1  
                                                a5                                           ee                                               a3  
                                                a6                                           ff                                               a5  
                                                ......  
                                    也许子节点下面还有子节点,也许没有,怎么样用treeview控件循环显示出正确数据?  
发表于:2008-01-09 14:06:101楼 得分:0
循环递归
发表于:2008-01-09 14:12:272楼 得分:0
怎么样写,有一点代码么?
发表于:2008-01-09 15:16:553楼 得分:0
给我你的邮箱,给你一个示例代码。
发表于:2008-01-09 15:22:164楼 得分:0
public   sub   tree_change()     '定义添加树状列表的函数
    dim   key   as   string,   text   as   string     'string   为字符型。
    dim   node1   as   node,   node2   as   node,   node3   as   node     '定义一个节点变量。
    adodc1.recordsource   =   "select   *   from   spzl   where   lbbs='是'   order   by   lbbh   asc"           '返回一个记录集的查询。
    adodc1.refresh
    'refresh   方法(ado):更新集合中的对象以便反映来自并特定于提供者的对象。refresh   方法根据从中调用的不同集合而完成不同的任务。
    treeview1.nodes.clear     '使treeview控件里面的内容清空。不先清空的话,第2遍执行以下代码时就会警告“集合中的关键字不唯一”。
    if   adodc1.recordset.recordcount   >   0   then               'recordcount   属性(ado):指示   recordset   对象中记录的当前数目。返回长整型值。
          adodc1.recordset.movefirst             '使用   movefirst   方法将当前记录位置移动到   recordset   中的第一个记录。
          do   while   adodc1.recordset.eof   =   false
          '如果当前记录位于   recordset   对象的最后一个记录之后,eof   属性将返回   true,而当前记录为   recordset   对象的最后一个记录或位于其前,则将返回   false。
                if   len(trim(adodc1.recordset.fields("lbbh")))   =   2   then     '类别编号
                'len   函数:返回   long   长整型,其中包含字符串内字符的数目,或是存储一变量所需的字节数。
                'ltrim、rtrim与trim函数:返回variant变体型(string字符型),其中包含指定字符串的拷贝,没有前导空白(ltrim)、尾随空白(rtrim)或前导和尾随空白(trim)。
                'fields   集合(ado):fields   集合包含   recordset   对象的所有   field   对象。每个   field   对象对应   recordset   中的一列。
                      key   =   "id"   &   trim(adodc1.recordset.fields("spzlid"))     '商品资料id
                      text   =   trim(adodc1.recordset.fields("lbbh"))   &   "   "   &   trim(adodc1.recordset.fields("mc"))     '类别编号,名称
                      '&   运算符:用来强制两个表达式作字符串连接。str(
                              '语法:result   =   expression1   &   expression2
                              'result   部分:必需的;任何   string字符型   或   variant变体型   变量。expression1与expression2部分:必需的;任何表达式。
                      set   node1   =   treeview1.nodes.add(,   ,   key,   text,   1,   2)   'key值不能用数字开头,不然会弹出警告“无效的关键字”。
                      'set   语句:将对象引用赋给变量或属性。
  'add   方法(nodes   集合):在   treeview   控件的   nodes   集合中添加一个   node   对象。
          '语法:object.add(relative,   relationship,   key,   text,   image,   selectedimage)
                    'object:必需的。对象表达式,其值是“应用于”列表中的一个对象。
                    'relative:可选的。已存在的   node   对象的索引号或键值。新节点与已存在的节点间的关系,可在下一个参数   relationship   中找到。
                    'relationship:可选的。指定的   node   对象的相对位置,如设置值中所述。
                    'key:可选的。唯一的字符串,可用于用   item   方法检索   node。
                    'text:必需的。在   node   中出现的字符串。
                    'image:可选的。在关联的   imagelist   控件中的图像的索引。
                    'selectedimage:可选的。在关联的   imagelist   控件中的图像的索引,在   node   被选中时显示。
                end   if
                if   len(trim(adodc1.recordset.fields("lbbh")))   =   4   then
                      key   =   "id"   &   trim(adodc1.recordset.fields("spzlid"))
                      text   =   trim(adodc1.recordset.fields("lbbh"))   &   "   "   &   trim(adodc1.recordset.fields("mc"))
                      set   node2   =   treeview1.nodes.add(node1.index,   tvwchild,   key,   text,   1,   2)
                      'index   属性(split   对象):返回对选定拆分的索引。
                      'tvwchild   常数:(缺省)子节点。该   node   成为在   relative   中被命名的节点的子节点。
                end   if
                if   len(trim(adodc1.recordset.fields("lbbh")))   =   6   then
                      key   =   "id"   &   trim(adodc1.recordset.fields("spzlid"))
                      text   =   trim(adodc1.recordset.fields("lbbh"))   &   "   "   &   trim(adodc1.recordset.fields("mc"))
                      set   node3   =   treeview1.nodes.add(node2.index,   tvwchild,   key,   text,   1,   2)
                end   if
                adodc1.recordset.movenext
          loop
    end   if
end   sub
发表于:2008-01-09 15:58:495楼 得分:0
正在学习用treeview,发个正在做的代码,应该有用。
private   sub   form_load()
    call   loadtree
end   sub
private   function   readtext(byval   txtfile   as   string)   as   string
        dim   txtstr   as   string
    open   txtfile   for   input   as   #1
    dim   txttemp   as   string
    txtstr   =   ""
    do   while   not   eof(1)       '   循环至文件尾。
        line   input   #1,   txttemp     '   将数据读入变量。
        txtstr   =   txtstr   &   txttemp   &   chr(13)   &   chr(10)
    loop
    close   #1
    readtext   =   txtstr
end   function
private   sub   loadtree()
        dim   mynod   as   node
        dim   aaa   as   string
        dim   i,   j,   k   as   integer
        dim   arraytemp()   as   string
        dim   arraystr()   as   string
        treeview1.nodes.clear
        set   mynod   =   treeview1.nodes.add(,   ,   "tuopu",   "网络拓扑图")
        aaa   =   readtext("loadtreev.txt")
        arraytemp   =   split(aaa,   vbcrlf)
        i   =   ubound(arraytemp)   -   1
        for   j   =   0   to   i
                arraystr   =   split(arraytemp(j),   " ¦")
                set   mynod   =   treeview1.nodes.add(arraystr(0),   tvwchild,   arraystr(1),   arraystr(2))
        next   j
        treeview1.nodes(1).expanded   =   true
end   sub
loadtreev.txt文本文件内容:
a1 ¦aa ¦r      
a2 ¦bb ¦r      
a3 ¦cc ¦a1      
a4 ¦dd ¦a1      
a5 ¦ee ¦a3      
a6 ¦ff ¦a5    
发表于:2008-01-11 13:36:296楼 得分:0
完整示例代码

'类clssubject代码
private   m_parentsubject   as   clssubject
private   m_subjectid   as   string
private   m_subjectname   as   string


public   property   get   parentsubject()   as   clssubject
        set   parentsubject   =   m_parentsubject
end   property

public   property   let   parentsubject(byval   value   as   clssubject)
       
        set   m_parentsubject   =   value
end   property


public   property   get   subjectid()   as   string
        subjectid   =   m_subjectid
end   property

public   property   let   subjectid(byval   value   as   string)
          m_subjectid   =   value
end   property


public   property   get   subjectname()   as   string
        subjectname   =   m_subjectname
end   property

public   property   let   subjectname(byval   value   as   string)
          m_subjectname   =   value
end   property

发表于:2008-01-11 13:38:297楼 得分:0
'集合类clssubjects,注意newenum的过程标识符须设置成-4
option   explicit


private   m_collection   as   collection


public   function   add(byval   id   as   string,   byval   name   as   string,   byval   parent   as   clssubject)   as   clssubject

        dim   tempsubject   as   new   clssubject
        on   error   goto   objecterr
        if   trim(name)   =   ""   or   trim(id)   =   ""   then
                set   add   =   nothing
                exit   function
        end   if
        '增加新的subject
        tempsubject.subjectid   =   id
        tempsubject.subjectname   =   name
        tempsubject.parentsubject   =   parent
        m_collection.add   tempsubject,   id
       
        set   add   =   tempsubject
        exit   function
objecterr:
        set   add   =   nothing
end   function
public   property   get   count()   as   long
      count   =   m_collection.count
end   property
public   sub   clear()
        dim   i   as   integer
        for   i   =   1   to   m_collection.count
                m_collection.remove   i
        next
end   sub
public   sub   remove(byval   index   as   variant)
        on   error   resume   next
        m_collection.remove   index
end   sub
public   property   get   item(byval   index   as   variant)   as   clssubject
        on   error   goto   itemerr
      set   item   =   m_collection.item(index)
      exit   property
itemerr:
      set   item   =   nothing
end   property
public   property   get   newenum()   as   iunknown
        set   newenum   =   m_collection.[_newenum]
end   property

private   sub   class_initialize()
        set   m_collection   =   new   collection
end   sub

private   sub   class_terminate()
        set   m_collection   =   nothing
end   sub


发表于:2008-01-11 13:39:408楼 得分:0
'窗体代码

dim   msubjects   as   clssubjects

private   sub   command1_click()


'显示树
               
  dim   tempsubject   as   clssubject
 
  if   msubjects.count   <>   0   then
 
  treeview1.nodes.clear
 
  for   each   tempsubject   in   msubjects
       
        call   addsubjecttotree(tempsubject)
  next
 
end   if
end   sub

private   function   addsubjecttotree(byval   subject   as   clssubject)   as   node

        '判断是否已添加
       
        dim   nd   as   node
        on   error   resume   next
        set   nd   =   treeview1.nodes.item(subject.subjectid)
        if   nd   is   nothing   then
                '判断是否有父科目
                if   not   (subject.parentsubject   is   nothing)   then
                       
                        set   nd   =   addsubjecttotree(subject.parentsubject)   '递归调用添加父目录
                        set   nd   =   treeview1.nodes.add(nd.key,   tvwchild,   subject.subjectid,   subject.subjectname)
                else
                        set   nd   =   treeview1.nodes.add(,   ,   subject.subjectid,   subject.subjectname)
                end   if
        end   if
          set   addsubjecttotree   =   nd
end   function
private   sub   form_load()
       
        set   msubjects   =   new   clssubjects
       
'
'a1               aa                   r
'a2               bb                   r
'a3               cc                   a1
'a4               dd                   a1
'a5               ee                   a3
'a6               ff                   a5

'添加科目

msubjects.add   "a1",   "aa",   nothing
msubjects.add   "a2",   "bb",   nothing
msubjects.add   "a3",   "cc",   msubjects("a1")
msubjects.add   "a4",   "dd",   msubjects("a1")
msubjects.add   "a5",   "ee",   msubjects("a3")
msubjects.add   "a6",   "ff",   msubjects("a5")
end   sub
发表于:2008-01-11 13:43:269楼 得分:0
上面的示例代码有2个比较实用的技术:

1、如何创建自己的强类型集合类;

2、如何使用递归调用;
发表于:2008-01-12 00:42:3010楼 得分:0

帮顶下啦
发表于:2008-01-12 10:30:2511楼 得分:0
up


快速检索

最新资讯
热门点击