Vb.net 将树视图设置为特定位置

Vb.net 将树视图设置为特定位置,vb.net,treeview,Vb.net,Treeview,我的C:驱动器中有一个文件夹,我希望在加载表单后立即访问该文件夹。我不想每次都滚动打开每个节点。我想使用一个树状视图,因为我知道如何使用其中的许多功能,它将符合我的目的 我将用一个基本文件结构给您举一个我想要的例子: C:\Users\user\Documents\Visual Studio 2010\Projects 如果我要通过整个树视图访问它,这将需要许多节点才能访问。我希望我的treeview从开始,因此主节点是 计划 如何执行此操作?以下是一个假设节点名称是文件夹的完整路径的示例: P

我的C:驱动器中有一个文件夹,我希望在加载表单后立即访问该文件夹。我不想每次都滚动打开每个节点。我想使用一个树状视图,因为我知道如何使用其中的许多功能,它将符合我的目的

我将用一个基本文件结构给您举一个我想要的例子: C:\Users\user\Documents\Visual Studio 2010\Projects

如果我要通过整个树视图访问它,这将需要许多节点才能访问。我希望我的treeview从开始,因此主节点是 计划


如何执行此操作?

以下是一个假设节点名称是文件夹的完整路径的示例:

Protected Overrides Sub OnLoad(e As EventArgs)
  Dim name As String = "c:\users\blairg\documents\visual studio 2010\projects"

  Dim testNode As New TreeNode("Projects")
  testNode.Name = name
  TreeView1.Nodes.Add(testNode)

  Dim node() As TreeNode = TreeView1.Nodes.Find(name, True)
  If node.Count = 1 Then
    TreeView1.SelectedNode = node(0)
  End If

  MyBase.OnLoad(e)
End Sub

我相信以上的答案会奏效。然而,我通过以下方式解决了问题:

        Dim backupfolder As String = netpath & "\MANUFPC BACKUP PROCESS\" & site & "\" & factory & "\" & line & "\" & pc

        Dim mRootNode As New TreeNode
        mRootNode.Text = pc
        mRootNode.Tag = backupfolder
        mRootNode.Nodes.Add("*DUMMY*")
        'adds plus icon to allow extension
        backupFolderDirectory.Nodes.Add(mRootNode)
然后是另外两个功能:

Private Sub TreeView1_BeforeCollapse(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles backupFolderDirectory.BeforeCollapse
    ' clear the node that is being collapsed
    e.Node.Nodes.Clear()
    ' add a dummy TreeNode to the node being collapsed so it is expandable
    e.Node.Nodes.Add("*DUMMY*")
End Sub

Private Sub TreeView1_BeforeExpand(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs) Handles backupFolderDirectory.BeforeExpand
    ' clear the expanding node so we can re-populate it, or else we end up with duplicate nodes
    e.Node.Nodes.Clear()

    ' get the directory representing this node
    Dim mNodeDirectory As DirectoryInfo
    mNodeDirectory = New DirectoryInfo(e.Node.Tag.ToString)
    ' add each subdirectory from the file system to the expanding node as a child node
    Try
        For Each mDirectory As DirectoryInfo In mNodeDirectory.GetDirectories
            ' declare a child TreeNode for the next subdirectory
            Dim mDirectoryNode As New TreeNode
            Dim mystring(1) As String
            mystring(0) = mDirectory.FullName
            mystring(1) = "directory"
            ' store the full path to this directory in the child TreeNode's Tag property
            mDirectoryNode.Tag = mystring(0)
            ' set the child TreeNodes's display text
            mDirectoryNode.Text = mDirectory.Name
            ' add a dummy TreeNode to this child TreeNode to make it expandable
            mDirectoryNode.Nodes.Add("*DUMMY*")
            ' add this child TreeNode to the expanding TreeNode
            e.Node.Nodes.Add(mDirectoryNode)
        Next

        For Each mFiles As FileInfo In mNodeDirectory.GetFiles
            ' declare a child TreeNode for the next subdirectory
            Dim mFileNode As New TreeNode
            Dim mystring(1) As String
            mystring(0) = mFiles.FullName
            mystring(1) = "file"
            ' store the full path to this directory in the child TreeNode's Tag property
            mFileNode.Tag = mystring(0)
            ' set the child TreeNodes's display text
            mFileNode.Text = mFiles.Name
            ' add this child TreeNode to the expanding TreeNode
            e.Node.Nodes.Add(mFileNode)
        Next

    Catch ex As IOException
        'sets up 2 different exceptions then the last one catches other exceptions that could be made from adding folder/files etc
        e.Node.Remove()
        MsgBox("Device/Folder not accessible", MsgBoxStyle.OkOnly, "Device not Ready")
    Catch exc As NullReferenceException
        e.Node.Remove()
        MsgBox("Sorry this File/Folder can not be added", MsgBoxStyle.OkOnly, "Sorry")
    Catch exce As Exception
        e.Node.Remove()
        MsgBox("Device/Folder not accessible", MsgBoxStyle.OkOnly, "Device not Ready")
    End Try
End Sub

呃,让根节点成为“Projects”而不是“C”:?我没有想过这样做,因为我不确定它是如何获得根节点的。回到我现有的一个,发现我实际上没有意识到。。。。哈哈。修正了我自己去做我想做的事!我没有设法让这种方式工作,但是正如BigYellow建议的那样,我修改了现有的方式,并将根节点放到文件路径中。我把它改成了我想要的东西。非常感谢您的帮助:)