VB.Net Tutorial/GUI/TreeView — различия между версиями

Материал из VB Эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 16:40, 26 мая 2010

Add and remove tree nodes

Imports System.Windows.Forms
public class TreeViewAddDeleteItem
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Private components As System.ruponentModel.IContainer
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents Button2 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.Button1 = New System.Windows.Forms.Button
        Me.Button2 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        "
        "TreeView1
        "
        Me.TreeView1.ImageIndex = -1
        Me.TreeView1.Location = New System.Drawing.Point(48, 16)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("a", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Visual Basic"), New System.Windows.Forms.TreeNode("Visual C++"), New System.Windows.Forms.TreeNode("Delphi"), New System.Windows.Forms.TreeNode("Java")}), New System.Windows.Forms.TreeNode("B", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("B1"), New System.Windows.Forms.TreeNode("B2"), New System.Windows.Forms.TreeNode("B3"), New System.Windows.Forms.TreeNode("B4")})})
        Me.TreeView1.SelectedImageIndex = -1
        Me.TreeView1.Size = New System.Drawing.Size(200, 168)
        Me.TreeView1.TabIndex = 0
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(48, 200)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(80, 24)
        Me.Button1.TabIndex = 1
        Me.Button1.Text = "Add"
        "
        "Button2
        "
        Me.Button2.Location = New System.Drawing.Point(176, 200)
        Me.Button2.Name = "Button2"
        Me.Button2.Size = New System.Drawing.Size(80, 24)
        Me.Button2.TabIndex = 2
        Me.Button2.Text = "Delete"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 238)
        Me.Controls.Add(Me.Button2)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TreeView1)
        Me.ResumeLayout(False)
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim str As New TreeNode
        TreeView1.SelectedNode.Nodes.Add("New")
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TreeView1.Nodes.Remove(TreeView1.SelectedNode)
    End Sub
End Class

Build file tree recursively

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class BuildFileTree
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Private Sub ActTraverse_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles ActTraverse.Click
        If (My.ruputer.FileSystem.DirectoryExists(StartPath.Text) = False) Then
            Exit Sub
        End If
        PathTree.Nodes.Clear()
        BuildDirectoryTree(Nothing, StartPath.Text)
    End Sub
    Private Sub BuildDirectoryTree(ByVal fromNode As TreeNode,ByVal basePath As String)
        Dim newDirectory As TreeNode
        Dim justTheSubdirectory As String
        For Each oneDirectory As String In My.ruputer.FileSystem.GetDirectories(basePath)
            justTheSubdirectory = My.ruputer.FileSystem.GetName(oneDirectory)
            If (fromNode Is Nothing) Then
                newDirectory = PathTree.Nodes.Add( justTheSubdirectory)
            Else
                newDirectory = fromNode.Nodes.Add(justTheSubdirectory)
            End If
            BuildDirectoryTree(newDirectory, My.ruputer.FileSystem.rubinePath(basePath, justTheSubdirectory))
        Next oneDirectory
    End Sub
End Class

<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _
Partial Class Form1
    Inherits System.Windows.Forms.Form
    "Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.ActTraverse = New System.Windows.Forms.Button
        Me.StartPath = New System.Windows.Forms.TextBox
        Me.PathTree = New System.Windows.Forms.TreeView
        Me.Label1 = New System.Windows.Forms.Label
        Me.Label2 = New System.Windows.Forms.Label
        Me.SuspendLayout()
        "
        "ActTraverse
        "
        Me.ActTraverse.Location = New System.Drawing.Point(326, 32)
        Me.ActTraverse.Name = "ActTraverse"
        Me.ActTraverse.Size = New System.Drawing.Size(75, 23)
        Me.ActTraverse.TabIndex = 2
        Me.ActTraverse.Text = "Traverse"
        Me.ActTraverse.UseVisualStyleBackColor = True
        "
        "StartPath
        "
        Me.StartPath.Location = New System.Drawing.Point(72, 8)
        Me.StartPath.Name = "StartPath"
        Me.StartPath.Size = New System.Drawing.Size(328, 20)
        Me.StartPath.TabIndex = 1
        "
        "PathTree
        "
        Me.PathTree.Location = New System.Drawing.Point(72, 64)
        Me.PathTree.Name = "PathTree"
        Me.PathTree.Size = New System.Drawing.Size(329, 184)
        Me.PathTree.TabIndex = 4
        "
        "Label1
        "
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(8, 10)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(57, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Start Path:"
        "
        "Label2
        "
        Me.Label2.AutoSize = True
        Me.Label2.Location = New System.Drawing.Point(8, 66)
        Me.Label2.Name = "Label2"
        Me.Label2.Size = New System.Drawing.Size(57, 13)
        Me.Label2.TabIndex = 3
        Me.Label2.Text = "Path Tree:"
        "
        "Form1
        "
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(411, 260)
        Me.Controls.Add(Me.Label2)
        Me.Controls.Add(Me.Label1)
        Me.Controls.Add(Me.PathTree)
        Me.Controls.Add(Me.StartPath)
        Me.Controls.Add(Me.ActTraverse)
        Me.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle
        Me.MaximizeBox = False
        Me.Name = "Form1"
        Me.Text = "Directory Traversal"
        Me.ResumeLayout(False)
        Me.PerformLayout()
    End Sub
    Friend WithEvents ActTraverse As System.Windows.Forms.Button
    Friend WithEvents StartPath As System.Windows.Forms.TextBox
    Friend WithEvents PathTree As System.Windows.Forms.TreeView
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
End Class

Creating Tree Views in Code

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class TreeViewCreationInCode
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
    Dim TreeView1 As TreeView
    Public Sub New()
        MyBase.New()
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 293)
        TreeView1 = New TreeView
        TreeView1.Location = New Point(75, 100)
        TreeView1.Size = New Size(150, 150)
        Controls.Add(TreeView1)
        TreeView1.Nodes.Clear()
        Dim RootNode = New TreeNode("Root")
        TreeView1.Nodes.Add(RootNode)
        TreeView1.Nodes(0).Nodes.Add(New TreeNode("Node 1"))
        For intLoopIndex As Integer = 2 To 3
            TreeView1.Nodes(0).Nodes(0).Nodes.Add(New _
                TreeNode("Node" & Str(intLoopIndex)))
        Next intLoopIndex
        TreeView1.Nodes(0).Nodes.Add(New TreeNode("Node 4"))
        For intLoopIndex As Integer = 5 To 6
            TreeView1.Nodes(0).Nodes(1).Nodes.Add(New _
                TreeNode("Node" & Str(intLoopIndex)))
        Next intLoopIndex
    End Sub
End Class

Tree Node selection event

Imports System.Windows.Forms
public class SplitterControl
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    Private components As System.ruponentModel.IContainer
    Friend WithEvents Splitter1 As System.Windows.Forms.Splitter
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents ImageList1 As System.Windows.Forms.ImageList
    Friend WithEvents Splitter2 As System.Windows.Forms.Splitter
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Splitter3 As System.Windows.Forms.Splitter
    Friend WithEvents ListView1 As System.Windows.Forms.ListView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.ruponents = New System.ruponentModel.Container
        Me.Splitter1 = New System.Windows.Forms.Splitter
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.ImageList1 = New System.Windows.Forms.ImageList(Me.ruponents)
        Me.Splitter2 = New System.Windows.Forms.Splitter
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.Label1 = New System.Windows.Forms.Label
        Me.Splitter3 = New System.Windows.Forms.Splitter
        Me.ListView1 = New System.Windows.Forms.ListView
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        "
        "Splitter1
        "
        Me.Splitter1.Location = New System.Drawing.Point(0, 0)
        Me.Splitter1.Name = "Splitter1"
        Me.Splitter1.Size = New System.Drawing.Size(3, 266)
        Me.Splitter1.TabIndex = 0
        Me.Splitter1.TabStop = False
        "
        "TreeView1
        "
        Me.TreeView1.Dock = System.Windows.Forms.DockStyle.Left
        Me.TreeView1.Location = New System.Drawing.Point(3, 0)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("A", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Visual Basic"), New System.Windows.Forms.TreeNode("Visual C++"), New System.Windows.Forms.TreeNode("Delphi"), New System.Windows.Forms.TreeNode("Java")}), New System.Windows.Forms.TreeNode("B", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("1"), New System.Windows.Forms.TreeNode("2"), New System.Windows.Forms.TreeNode("3"), New System.Windows.Forms.TreeNode("4")})})
        Me.TreeView1.Size = New System.Drawing.Size(101, 266)
        Me.TreeView1.TabIndex = 1
        "
        "Splitter2
        "
        Me.Splitter2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Splitter2.Location = New System.Drawing.Point(104, 0)
        Me.Splitter2.Name = "Splitter2"
        Me.Splitter2.Size = New System.Drawing.Size(8, 266)
        Me.Splitter2.TabIndex = 2
        Me.Splitter2.TabStop = False
        "
        "Panel1
        "
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
        Me.Panel1.Controls.Add(Me.ListView1)
        Me.Panel1.Controls.Add(Me.Splitter3)
        Me.Panel1.Controls.Add(Me.Label1)
        Me.Panel1.Dock = System.Windows.Forms.DockStyle.Top
        Me.Panel1.Location = New System.Drawing.Point(112, 0)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(248, 264)
        Me.Panel1.TabIndex = 3
        "
        "Label1
        "
        Me.Label1.Dock = System.Windows.Forms.DockStyle.Top
        Me.Label1.Location = New System.Drawing.Point(0, 0)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(244, 32)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Label1"
        "
        "Splitter3
        "
        Me.Splitter3.Dock = System.Windows.Forms.DockStyle.Top
        Me.Splitter3.Location = New System.Drawing.Point(0, 32)
        Me.Splitter3.Name = "Splitter3"
        Me.Splitter3.Size = New System.Drawing.Size(244, 8)
        Me.Splitter3.TabIndex = 1
        Me.Splitter3.TabStop = False
        "
        "ListView1
        "
        Me.ListView1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.ListView1.Location = New System.Drawing.Point(0, 40)
        Me.ListView1.Name = "ListView1"
        Me.ListView1.Size = New System.Drawing.Size(244, 220)
        Me.ListView1.TabIndex = 2
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(360, 266)
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.Splitter2)
        Me.Controls.Add(Me.TreeView1)
        Me.Controls.Add(Me.Splitter1)
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)
    End Sub
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        Dim childnum, index, i As Int32
        childnum = TreeView1.SelectedNode.GetNodeCount(False)
        index = TreeView1.SelectedNode.Index
        index = index + 1
        ListView1.Items.Clear()
        For i = 0 To childnum - 1
            ListView1.Items.Add(index.ToString + "." + i.ToString, 1)
        Next
        Label1.Text = TreeView1.SelectedNode.FullPath
    End Sub
End Class

TreeView CheckBox

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
public class TreeViewSelectionCheckBoxes
   public Shared Sub Main
        Application.Run(New Form1)
   End Sub
End class
Public Class Form1
    Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
    Public Sub New()
        MyBase.New()
        "This call is required by the Windows Form Designer.
        InitializeComponent()
        "Add any initialization after the InitializeComponent() call
    End Sub
    "Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub
    "Required by the Windows Form Designer
    Private components As System.ruponentModel.IContainer
    "NOTE: The following procedure is required by the Windows Form Designer
    "It can be modified using the Windows Form Designer.  
    "Do not modify it using the code editor.
    Friend WithEvents TreeView1 As System.Windows.Forms.TreeView
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.TreeView1 = New System.Windows.Forms.TreeView
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        "
        "TreeView1
        "
        Me.TreeView1.ImageIndex = -1
        Me.TreeView1.Location = New System.Drawing.Point(88, 64)
        Me.TreeView1.Name = "TreeView1"
        Me.TreeView1.Nodes.AddRange(New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Root", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node 1", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node 2"), New System.Windows.Forms.TreeNode("Node 3")}), New System.Windows.Forms.TreeNode("Node 4", New System.Windows.Forms.TreeNode() {New System.Windows.Forms.TreeNode("Node 5"), New System.Windows.Forms.TreeNode("Node 6")})})})
        Me.TreeView1.SelectedImageIndex = -1
        Me.TreeView1.Size = New System.Drawing.Size(121, 120)
        Me.TreeView1.TabIndex = 0
        "
        "TextBox1
        "
        Me.TextBox1.Location = New System.Drawing.Point(88, 200)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(120, 20)
        Me.TextBox1.TabIndex = 1
        Me.TextBox1.Text = ""
        "
        "Button1
        "
        Me.Button1.Location = New System.Drawing.Point(88, 232)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(120, 23)
        Me.Button1.TabIndex = 3
        Me.Button1.Text = "Show Check Boxes"
        "
        "Form1
        "
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox1)
        Me.Controls.Add(Me.TreeView1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)
    End Sub
#End Region
    Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
        TextBox1.Text = e.Node.Text & " was selected."
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TreeView1.CheckBoxes = True
    End Sub
    Private Sub TreeView1_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck
        If e.Node.Checked Then
            TextBox1.Text = e.Node.Text & " is checked."
        Else
            TextBox1.Text = e.Node.Text & " is not checked."
        End If
    End Sub
End Class

Using a TreeView to display the directory structure

Imports System.IO
Imports System.Windows.Forms
public class FileTreeView
   public Shared Sub Main
        Application.Run(New FrmTreeViewDirectory)
   End Sub
End class

Public Class FrmTreeViewDirectory
   Inherits Form
#Region " Windows Form Designer generated code "
   Public Sub New()
      MyBase.New()
      "This call is required by the Windows Form Designer.
      InitializeComponent()
      "Add any initialization after the InitializeComponent() 
      "call
   End Sub
   "Form overrides dispose to clean up the component list.
   Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
      If Disposing Then
         If Not (components Is Nothing) Then
            components.Dispose()
         End If
      End If
      MyBase.Dispose(Disposing)
   End Sub
   " contains view of c: drive directory structure
   Friend WithEvents treDirectory As System.Windows.Forms.TreeView
   "Required by the Windows Form Designer
   Private components As System.ruponentModel.Container
   "NOTE: The following procedure is required by the Windows Form Designer
   "It can be modified using the Windows Form Designer.  
   "Do not modify it using the code editor.
   <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
      Me.treDirectory = New System.Windows.Forms.TreeView()
      Me.SuspendLayout()
      "
      "treDirectory
      "
      Me.treDirectory.Dock = System.Windows.Forms.DockStyle.Fill
      Me.treDirectory.ImageIndex = -1
      Me.treDirectory.Name = "treDirectory"
      Me.treDirectory.SelectedImageIndex = -1
      Me.treDirectory.Size = New System.Drawing.Size(296, 293)
      Me.treDirectory.TabIndex = 0
      "
      "FrmTreeViewDirectory
      "
      Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
      Me.ClientSize = New System.Drawing.Size(296, 293)
      Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.treDirectory})
      Me.Name = "FrmTreeViewDirectory"
      Me.Text = "TreeViewDirectoryStructure"
      Me.ResumeLayout(False)
   End Sub
#End Region
   Public Sub PopulateTreeView(ByVal directoryValue As String, _
      ByVal parentNode As TreeNode)
      Dim directoryArray As String() = Directory.GetDirectories(directoryValue)
      If directoryArray.Length <> 0 Then 
         Dim currentDirectory As String
         For Each currentDirectory In directoryArray
            Dim myNode As TreeNode = New TreeNode(currentDirectory)
            parentNode.Nodes.Add(myNode)
"            PopulateTreeView(currentDirectory, myNode)
         Next
      End If
   End Sub 
   Private Sub FrmTreeViewDirectory_Load(ByVal sender As Object, _
      ByVal e As System.EventArgs) Handles MyBase.Load
      treDirectory.Nodes.Add("C:")
      PopulateTreeView("C:\", treDirectory.Nodes(0))
   End Sub
End Class