重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
给你一段我曾经写的代码,主要是生成TreeView的节点!有什么不懂的地方你就提!
阿尔山网站建设公司成都创新互联,阿尔山网站设计制作,有大型网站制作公司丰富经验。已为阿尔山1000多家提供企业网站建设服务。企业网站搭建\成都外贸网站制作要多少钱,请找那个售后服务好的阿尔山做网站的公司定做!
Public Sub InitializeTreeView(ByVal strvXL As TreeView, ByVal vrsRecordTwo As DataSet)
Dim treBaseNodeOne As TreeNode
Dim treBaseNodeTwo As TreeNode
Dim strDWDM As String
m_rsRecordTwo = g_clsSybase.SelectSC_PD_SBCSB1()
'查找供电局信息
g_clsSybase.SelectRS_ZZJG_ZZJGDMB(m_rsRecordOne)
'添加根节点
Do While m_rsRecordOne.Read
strDWDM = m_rsRecordOne.GetString(0) '获取单位代码
treBaseNodeOne = New TreeNode
treBaseNodeOne.Text = m_rsRecordOne.GetString(1).Trim() '获取单位名称
treBaseNodeOne.Name = m_rsRecordOne.GetString(1)
treBaseNodeOne.Tag = ""
strvXL.Nodes.Add(treBaseNodeOne)
'添加树结构第二层
Dim drsTwo() As DataRow
drsTwo = m_rsRecordTwo.Tables(0).Select("SSDW='" strDWDM "' and SC__XLBM is null")
For Each dr As DataRow In drsTwo
treBaseNodeTwo = New TreeNode
treBaseNodeTwo.Text = dr.Item("XLMC").ToString.Trim
treBaseNodeTwo.Name = dr.Item("XLMC")
treBaseNodeTwo.Tag = dr.Item("XLBM")
treBaseNodeOne.Nodes.Add(treBaseNodeTwo)
Next
Loop
m_rsRecordOne.Close()
End Sub
预先准备三个图标文件,用于树型控件中显示磁盘符号和文件夹的图像之用。
1、窗体上添加控件如下:
组合框控件 ComboBox1,树型控件 TreeView1,列表框控件 ListBox1,图像列表控件 ImageList1。
选中TreeView1,设置其ImageList属性为ImageList1。
2、设置属性
选中图像列表控件 ImageList1,在属性窗口里,选中属性Images,单击三个小点按钮,出现图像集合编辑器窗口,单击[添加按钮],一一把准备好的图标文件进行添加,注意先后次序,如果不符合要求可以通过上下移动按钮重新改变次序。完成后单击[确定]。
运行图如下:
完整代码如下:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'添加系统所有磁盘目录符号
For Each MyDrive As String In Environment.GetLogicalDrives()
ComboBox1.Items.Add(MyDrive)
Next
'显示第一个磁盘符号
ComboBox1.Text = ComboBox1.Items(0)
End Sub
'递归过程添加目录树
Public Sub AddDirectory(ByVal strFatherPath As String, ByVal strPath As String, ByVal nodeFather As TreeNode)
Dim i As Integer
Dim Mynode As New TreeNode
'先添加本目录
Mynode.Text = Strings.Replace(strPath, strFatherPath "\", "", , 1)
'为节点指定未被选中时显示的图标
Mynode.ImageIndex = 1
'为节点指定被选中时显示的图标
Mynode.SelectedImageIndex = 2
nodeFather.Nodes.Add(Mynode)
Application.DoEvents()
Try
Dim str() As String = Directory.GetDirectories(strPath)
'递归遍历该目录的子文件夹
For i = 0 To str.GetUpperBound(0)
AddDirectory(strPath, str(i), Mynode)
Next
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Mynode = Nothing
End Sub
'根据给出的盘符添加目录树
Private Sub AddRootDirectory(ByVal DiscSymbol As String)
Dim Nynode As New TreeNode
'先把磁盘盘符添加到树中
TreeView1.Nodes.Clear()
Nynode.ImageIndex = 0
Nynode.Text = DiscSymbol
Nynode.SelectedImageIndex = -1
TreeView1.Nodes.Add(Nynode)
Dim i As Integer
'获取磁盘根目录下的文件夹
Dim str() As String = Directory.GetDirectories(DiscSymbol "\")
For i = 0 To str.GetUpperBound(0)
'调用递归过程遍历该文件夹里的所有子文件夹,并添加到树型控件
AddDirectory(DiscSymbol, str(i), Nynode)
Next
Nynode = Nothing
End Sub
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
'根据磁盘符号的变更,显示根目录里的文件
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(ComboBox1.Text)
ListBox1.Items.Add(MyFile)
Next
'根据磁盘符号的变更,重新显示目录树
Dim DiscSymbol As String
DiscSymbol = Microsoft.VisualBasic.Left(ComboBox1.Text, Len(ComboBox1.Text) - 1)
Call AddRootDirectory(DiscSymbol)
End Sub
'递归过程根据子目录寻找上级目录名--从而构成完整的目录路径
Private Sub AllPath(ByVal ThisNode As TreeNode, ByRef MyPathName As String)
If ThisNode.Level 1 Then
'该节点层数大于1,其父节点不是磁盘根目录
MyPathName = ThisNode.Parent.Text "\" MyPathName
Dim MyNode As TreeNode = ThisNode.Parent
Call AllPath(MyNode, MyPathName)
Else
'该节点层数等于1,其父节点就是磁盘根目录
MyPathName = ComboBox1.Text MyPathName
End If
End Sub
Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
'为了搜索选中的节点对应目录的文件,需要组成全路径
Dim MyAllPathName As String = TreeView1.SelectedNode.Text
Dim MyNode As TreeNode = TreeView1.SelectedNode
If TreeView1.SelectedNode.Level = 0 Then
'如果选中的是根节点
MyAllPathName = ComboBox1.Text
Else
'如果选中的是非根节点,调用递归过程组成全路径
Call AllPath(MyNode, MyAllPathName)
MyAllPathName = MyAllPathName "\"
End If
'根据路径,搜索文件名并显示
ListBox1.Items.Clear()
For Each MyFile As String In System.IO.Directory.GetFiles(MyAllPathName)
ListBox1.Items.Add(MyFile)
Next
End Sub
End Class
你说的“树形菜单”是指下拉菜单(可以有子菜单)吗?如果是,VB.net提供了MenuStrip控件,在工具箱里能找到,直接就在对应位置打字就可以了,完全是“所见即所得”。
添加:(先在加一个contextMenu,再它的添加子菜单的click事件编程)
Try
’使TreeView可以被编辑
TreeView1.LabelEdit = True
‘判断你是不是选定的是不可编辑的节点,我这里工种节点不可以被编辑,只有工种下级的
各个工种名称可以被编辑
If Trim(TreeView1.SelectedNode.Text) = "工种" Then
‘添加节点
AddNode = New TreeNode("请输入新工种名字")
TreeView1.SelectedNode.Nodes.Add(AddNode)
TreeView1.ExpandAll()
AddNode.BeginEdit()
TreeView1.LabelEdit = True
NodeAdded = True
End If
Catch err As Exception
MsgBox(err.ToString)
End Try
删除与添加类似,只是如果你的节点名字从其他处(如数据库)得来,那么你还需要更新数据库
编辑:
Private Sub TreeView1_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.BeforeLabelEdit
TreeView1.LabelEdit = True ‘使可以编辑
AddNode = TreeView1.SelectedNode
End Sub
Private Sub TreeView1_AfterLabelEdit(ByVal sender As Object, ByVal e As System.windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.AfterLabelEdit
Try
‘此时你改完了节点名字
TreeView1.SelectedNode.EndEdit(True)
If e.Label Is Nothing Then
'do nothing
ElseIf e.Node.Text = "工种" Then ‘工种不能改
e.CancelEdit() = True
‘e.Node.Text ,e.Label.ToString 一个是改前的名字一个是该后的名字,具体哪个对
哪个请查MSDN
ElseIf Trim(e.Node.Text) "工种" And e.Node.Text e.Label.ToString Then
If MsgBox("此操作会导致当前工种中的所有人员的工种都被更改,是否确定?", MsgBoxStyle.YesNo + MsgBoxStyle.Information, "警告") = MsgBoxResult.Yes Then
。。。。 ‘我的更改
MsgBox("更改成功!", MsgBoxStyle.OKOnly, "提示")
'Call InitTree() ‘有时要重新把treeview初始化一遍,视需求定
End If
End If
Catch err As Exception
MsgBox(err.ToString)
End Try
End Sub
其他:
挡treeview得到焦点时你可以使用ContextMenu,反之ContextMenu禁用
Private Sub TreeView1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.GotFocus
TreeView1.ContextMenu = ContextMenu1
End Sub
Private Sub TreeView1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.LostFocus
TreeView1.ContextMenu = Nothing
End Sub
注意:这里没有在ContextMenu菜单添加“更改”项,而是直接更改:即左键单击节点表示
选中,再单击一下就可以编辑了,更改之后单击他处就完成更改,和你在windows中更改文
件名字相似。