vb.net TreeView 数目统计

office_2007 office_2007 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
本题是按照csdn网页会员的要求亲自编写的。

http://bbs.csdn.net/topics/390878463

原数据的视图


运行后统计各自节点的数目,包含本节点的数目合计统计。


此代码如下;

Public Class Class2
Private List1 As New List(Of Integer)
Private List2 As New List(Of Integer)
Private Dictionary1 As New Dictionary(Of Integer, List(Of Integer))
Private NextKey1 As Integer = 0
Private NextKey2 As Integer = 1
Private NextKey3 As Integer = 0
Private Nextkey4 As Integer = 0
Private Array1 As String()
Private TreeView1 As TreeView
Public Property _TreeView1() As TreeView
Get
Return TreeView1
End Get
Set(ByVal value As TreeView)
TreeView1 = value
End Set
End Property

Public Sub MySub()
Dim Sum1 As Integer = List1.Sum
ReadAllNode(TreeView1.Nodes)
ReadAllNode1(TreeView1.Nodes)
End Sub

Private Sub ReadAllNode(ByVal Nodes1 As TreeNodeCollection)
Dim Val1 As Integer
Dim Val2 As Integer
Dim Next1 As Integer = 1
Dim Str1 As String
Dim Str2 As String
Dim Str3 As String
Dim Array2 As String()
For Each S As TreeNode In Nodes1
Str2 = S.Text
Str3 = S.FullPath
' MsgBox(Str3)
Array2 = Str3.Split("\")
Val2 = Val(Array2.Last.Split("(").ElementAt(1))
List2.Add(Val2)
If S.Nodes.Count > 0 Then
ReadAllNode(S.Nodes)
End If
If String.Equals(Str2, Str3) Then
Str1 = Str2.Split("(").ElementAt(0)
TreeView1.Nodes.Item(NextKey1).Text = Str1 & "(" & List1.Sum & ")"
Dictionary1.Add(NextKey2, List2)
List1 = New List(Of Integer)
List2 = New List(Of Integer)
NextKey1 += 1
NextKey2 += 1
Else
Val1 = Val(Str2.Split("(").ElementAt(1))
List1.Add(Val1)
End If
Next

End Sub
Private Sub ReadAllNode1(ByVal Nodes1 As TreeNodeCollection)
Dim Val1 As Integer
Dim Str1 As String
Dim Str2 As String
Dim Str3 As String
For Each S As TreeNode In Nodes1
Str2 = S.Text
Str3 = S.FullPath
Array1 = S.FullPath.Split("\")
Val1 = S.Nodes.Count
If String.Equals(Str2, Str3) Then
NextKey3 += 1
Nextkey4 = 1
End If

If Val1 > 0 Then
S.Text = Dictionary1.Item(NextKey3).Skip(Nextkey4 - 1).Sum.ToString
End If
Nextkey4 += 1
If S.Nodes.Count > 0 Then
ReadAllNode1(S.Nodes)
End If
Next
End Sub
End Class

Public Class Form1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Cla1 As New Class1
Cla1._TreeView1 = Me.TreeView1
Cla1.MySub()
End Sub
End Class


第二个效果图是只统计各节点下的所有数目,不包含本节点。

效果图如下;



此代码如下;


Public Class Class1
Private List1 As New List(Of Integer)
Private List2 As New List(Of Integer)
Private Dictionary1 As New Dictionary(Of Integer, List(Of Integer))
Private NextKey1 As Integer = 0
Private NextKey2 As Integer = 1
Private NextKey3 As Integer = 0
Private Nextkey4 As Integer = 1
Private Array1 As String()
Private TreeView1 As TreeView
Public Property _TreeView1() As TreeView
Get
Return TreeView1
End Get
Set(ByVal value As TreeView)
TreeView1 = value
End Set
End Property

Public Sub MySub()
Dim Sum1 As Integer = List1.Sum
ReadAllNode(TreeView1.Nodes)
ReadAllNode1(TreeView1.Nodes)
End Sub

Private Sub ReadAllNode(ByVal Nodes1 As TreeNodeCollection)
Dim Val1 As Integer
Dim Val2 As Integer
Dim Next1 As Integer = 1
Dim Str1 As String
Dim Str2 As String
Dim Str3 As String
Dim Array2 As String()
For Each S As TreeNode In Nodes1
Str2 = S.Text
Str3 = S.FullPath
' MsgBox(Str3)
Array2 = Str3.Split("\")
Val2 = Val(Array2.Last.Split("(").ElementAt(1))
List2.Add(Val2)
If S.Nodes.Count > 0 Then
ReadAllNode(S.Nodes)
End If
If String.Equals(Str2, Str3) Then
Str1 = Str2.Split("(").ElementAt(0)
TreeView1.Nodes.Item(NextKey1).Text = Str1 & "(" & List1.Sum & ")"
Dictionary1.Add(NextKey2, List2)
List1 = New List(Of Integer)
List2 = New List(Of Integer)
NextKey1 += 1
NextKey2 += 1
Else
Val1 = Val(Str2.Split("(").ElementAt(1))
List1.Add(Val1)
End If
Next

End Sub
Private Sub ReadAllNode1(ByVal Nodes1 As TreeNodeCollection)
Dim Val1 As Integer
Dim Str2 As String
Dim Str3 As String
For Each S As TreeNode In Nodes1
Str2 = S.Text
Str3 = S.FullPath
Array1 = S.FullPath.Split("\")
Val1 = S.Nodes.Count
If String.Equals(Str2, Str3) Then
NextKey3 += 1
Nextkey4 = 1
End If
Nextkey4 += 1
If Val1 > 0 Then
S.Text = Dictionary1.Item(NextKey3).Skip(Nextkey4 - 1).Sum.ToString
End If

If S.Nodes.Count > 0 Then
ReadAllNode1(S.Nodes)
End If
Next
End Sub
End Class

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Cla1 As New Class1
Cla1._TreeView1 = Me.TreeView1
Cla1.MySub()
End Sub
End Class


...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部
office_2007
VB 16,718 成员 110,520 内容