扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
字段名 | 字段 | 类型说明 |
ID | 自动编号 | Key |
Code | String | 编码 |
Name | String | 名称 |
PID | Int | 父结点的ID |
CPtr | boolean | 是否有子结点 |
属性名 | 类型 | 说明 |
Value | Object | Key |
PID | Object | 父结点的ID |
CPtr | Boolean | 是否有子结点 |
属性名 | 类型 | 说明 |
Datasource | dataview | dbTreeVIew的数据源使用dataview,而不是object |
Value | Member | string值成员(数据源[dataview]的列名) |
Display | Member | string显示(在Text中)成员 |
Pid | Member | string父ID成员 |
CPtr | Member | string是否有子结点 |
Protected Property DataSource() As Object Get Return mDataView End Get Set(ByVal Value As Object) If Value Is Nothing Then Else mDataView = Value cm = CType(Me.BindingContext(mDataView), CurrencyManager) UpdateTreeView() End If End Set End Property Protected Property PidMember() As String Get Return mPidMember End Get Set(ByVal Value As String) mPidMember = Value End Set End Property Protected Property DisplayMember() As String Get Return Join(mDisplayMember, SplitChar) End Get Set(ByVal Value As String) mDisplayMember = Split(Value, SplitChar) End Set End Property '注意,这几个属性都是保护成员,必须在Init事件中设置: Public Sub Init(ByVal dispmember As String, ByVal valuemember As String, ByVal pidmember As String, ByVal cptrmember As String, ByVal datasource As DataView) Me.ValueMember = valuemember Me.DisplayMember = dispmember Me.PidMember = pidmember Me.CPtrMember = cptrmember Me.DataSource = datasource '取value最大值,新增时将value+1,保证关健值唯一。 Me.mDataView.Sort = Me.ValueMember Me.m_MaxID = Me.GetValue(Me.mDataView.Count - 1) End Sub |
Protected Overridable Function GetDisplay(ByVal Index As Integer) As Object Dim i As Integer Dim temp As String = "" For i = 0 To mDisplayMember.Length - 1 temp = temp & IIf(i > 0, LinkChar, "") & mDataView(Index)(mDisplayMember(i)) Next Return temp End Function |
其它检索值的函数请参见源程序。
生成树
UpdateTreeView调用私有方法FillTree来生成树,需要注意的,FillTree只是生成指定结点的子结点并将其添加到指定结点,而不是一次就将所有结点添加到树中,如果未指定结点(第一次填充时),只是添加顶层结点。
Private Sub FillTree(ByRef pnode As myTreeNode, Optional ByVal filter As String = "") mDataView.RowFilter = filter Dim i As Integer, icol As Integer Dim newnode As myTreeNode RemoveHandler cm.PositionChanged, AddressOf cm_PositionChanged Me.BeginUpdate() For i = 0 To mDataView.Count() - 1 newnode = New myTreeNode(GetDisplay(i), GetValue(i), GetPid(i), GetCPtr(i)) '当有子结点时,为这个结点添加一个空子结点 If newnode.CPtr Then Dim nullnode As New myTreeNode() nullnode.Value = NoExpandNodeValue newnode.Nodes.Add(nullnode) End If If pnode Is Nothing Then Me.Nodes.Clear() Me.Nodes.Add(newnode) Else pnode.Nodes.Add(newnode) End If Next Me.EndUpdate() mDataView.RowFilter = "" AddHandler cm.PositionChanged, AddressOf cm_PositionChanged End Sub |
在展开有子结点的结点前,删除所有子结点,再用FillTree为待展开结点新增子结点。
Private Sub dbTreeView_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles MyBase.BeforeExpand '当是新增结点引起BeforeExpand事件时,直接退出。 If ExpandWhenAddNode Then Exit Sub '在展开结点前更新子结点 Dim currentnode As myTreeNode = CType(e.Node, myTreeNode) With currentnode .Nodes.Clear() FillTree(currentnode, mPidMember & "= " & CInt(.Value)) End With End Sub |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者