VB.Net/GUI/TreeView
Содержание
Add Mouse Click event to a TreeView: display full path
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.Drawing Imports System.Windows.Forms Imports System.ruponentModel Imports System.Drawing.Drawing2D Imports System.IO Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(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 treeFood As System.Windows.Forms.TreeView <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.treeFood = New System.Windows.Forms.TreeView() Me.SuspendLayout() " "treeFood " Me.treeFood.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.treeFood.ImageIndex = -1 Me.treeFood.Location = New System.Drawing.Point(4, 4) Me.treeFood.Name = "treeFood" Me.treeFood.SelectedImageIndex = -1 Me.treeFood.Size = New System.Drawing.Size(284, 256) Me.treeFood.TabIndex = 0 " "TreeViewExample " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.treeFood}) Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "TreeViewExample" Me.Text = "TreeView Example" Me.ResumeLayout(False) End Sub
- End Region
Private Sub TreeViewExample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim node As TreeNode node = treeFood.Nodes.Add("Node") node.Nodes.Add("Leap 1") node.Nodes.Add("Leap 2") node = treeFood.Nodes.Add("Node 2") node.Nodes.Add("Leap 3") node.Nodes.Add("Leap 4") End Sub Private Sub treeFood_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeFood.AfterSelect If e.Action = TreeViewAction.ByMouse Then MessageBox.Show(e.Node.FullPath) End If End Sub
End Class
</source>
Add Node and Leap to a TreeView
<source lang="vbnet"> Imports System Imports System.Collections Imports System.Data Imports System.Drawing Imports System.Windows.Forms Imports System.ruponentModel Imports System.Drawing.Drawing2D Imports System.IO Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(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 treeFood As System.Windows.Forms.TreeView <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.treeFood = New System.Windows.Forms.TreeView() Me.SuspendLayout() " "treeFood " Me.treeFood.Anchor = (((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _ Or System.Windows.Forms.AnchorStyles.Left) _ Or System.Windows.Forms.AnchorStyles.Right) Me.treeFood.ImageIndex = -1 Me.treeFood.Location = New System.Drawing.Point(4, 4) Me.treeFood.Name = "treeFood" Me.treeFood.SelectedImageIndex = -1 Me.treeFood.Size = New System.Drawing.Size(284, 256) Me.treeFood.TabIndex = 0 " "TreeViewExample " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14) Me.ClientSize = New System.Drawing.Size(292, 266) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.treeFood}) Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "TreeViewExample" Me.Text = "TreeView Example" Me.ResumeLayout(False) End Sub
- End Region
Private Sub TreeViewExample_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim node As TreeNode node = treeFood.Nodes.Add("Node") node.Nodes.Add("Leap 1") node.Nodes.Add("Leap 2") node = treeFood.Nodes.Add("Node 2") node.Nodes.Add("Leap 3") node.Nodes.Add("Leap 4") End Sub
End Class
</source>
Custom Tree View
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.ruponentModel Imports System.Drawing.Drawing2D Imports System.IO Public Class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(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 "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. Private components As System.ruponentModel.IContainer Friend WithEvents tree As JobTree <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.ruponents = New System.ruponentModel.Container() Me.tree = New JobTree() Me.SuspendLayout() " "tree " Me.tree.Location = New System.Drawing.Point(8, 8) Me.tree.Name = "tree" Me.tree.Scrollable = False Me.tree.Size = New System.Drawing.Size(368, 288) Me.tree.TabIndex = 0 " "CustomJobUserTree " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 14) Me.ClientSize = New System.Drawing.Size(384, 310) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.tree}) Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Name = "CustomJobUserTree" Me.Text = "JobUser Tree" Me.ResumeLayout(False) End Sub
- End Region
Private Sub CustomJobUserTree_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load tree.AddJob("Job 1", JobTree.StatusType.InProgress) tree.AddJob("Job 2", JobTree.StatusType.Unassigned) tree.AddJob("Job 3", JobTree.StatusType.Unassigned) tree.AddJob("Job 4", JobTree.StatusType.Closed) tree.AddJob("Job 5", JobTree.StatusType.Closed)
End Sub
End Class Public Class JobTree
Inherits TreeView Public Enum StatusType Unassigned = 101 InProgress = 102 Closed = 103 End Enum Private nodeUnassigned As New TreeNode("Unassigned", 0, 0) Private nodeInProgress As New TreeNode("In Progress", 1, 1) Private nodeClosed As New TreeNode("Closed", 2, 2) Public Sub New() MyBase.New() MyBase.Nodes.Clear() If Me.DesignMode = False Then MyBase.Nodes.Add(nodeUnassigned) MyBase.Nodes.Add(nodeInProgress) MyBase.Nodes.Add(nodeClosed) End If End Sub Public Sub AddJob(ByVal name As String, ByVal status As StatusType) Dim nodeNew As New TreeNode(name, 3, 4) nodeNew.Tag = status Select Case status Case StatusType.Unassigned nodeUnassigned.Nodes.Add(nodeNew) Case StatusType.InProgress nodeInProgress.Nodes.Add(nodeNew) Case StatusType.Closed nodeClosed.Nodes.Add(nodeNew) End Select End Sub
End Class
</source>
Directory Tree
<source lang="vbnet"> Imports System.IO Imports System.Windows.Forms <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Form1
Inherits System.Windows.Forms.Form <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.SplitContainer1 = New System.Windows.Forms.SplitContainer Me.treeFiles = New System.Windows.Forms.TreeView Me.lstFiles = New System.Windows.Forms.ListBox Me.SplitContainer1.Panel1.SuspendLayout() Me.SplitContainer1.Panel2.SuspendLayout() Me.SplitContainer1.SuspendLayout() Me.SuspendLayout() " "SplitContainer1 " Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill Me.SplitContainer1.Location = New System.Drawing.Point(5, 5) Me.SplitContainer1.Name = "SplitContainer1" " "SplitContainer1.Panel1 " Me.SplitContainer1.Panel1.Controls.Add(Me.treeFiles) " "SplitContainer1.Panel2 " Me.SplitContainer1.Panel2.Controls.Add(Me.lstFiles) Me.SplitContainer1.Size = New System.Drawing.Size(429, 295) Me.SplitContainer1.SplitterDistance = 143 Me.SplitContainer1.TabIndex = 0 " "treeFiles " Me.treeFiles.Dock = System.Windows.Forms.DockStyle.Fill Me.treeFiles.Location = New System.Drawing.Point(0, 0) Me.treeFiles.Name = "treeFiles" Me.treeFiles.Size = New System.Drawing.Size(143, 295) Me.treeFiles.TabIndex = 1 " Me.lstFiles.Dock = System.Windows.Forms.DockStyle.Fill Me.lstFiles.IntegralHeight = False Me.lstFiles.Location = New System.Drawing.Point(0, 0) Me.lstFiles.Name = "lstFiles" Me.lstFiles.Size = New System.Drawing.Size(282, 295) " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(439, 305) Me.Controls.Add(Me.SplitContainer1) Me.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Padding = New System.Windows.Forms.Padding(5) Me.Text = "File Browser" Me.SplitContainer1.Panel1.ResumeLayout(False) Me.SplitContainer1.Panel2.ResumeLayout(False) Me.SplitContainer1.ResumeLayout(False) Me.ResumeLayout(False) End Sub Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer Friend WithEvents treeFiles As System.Windows.Forms.TreeView Friend WithEvents lstFiles As System.Windows.Forms.ListBox Private Sub Fill(ByVal Dir As DirectoryInfo, ByVal DirNode As TreeNode) Try For Each DirItem As DirectoryInfo In Dir.GetDirectories Dim NewNode As New TreeNode(DirItem.Name) DirNode.Nodes.Add(NewNode) NewNode.Nodes.Add("*") Next Catch End Try End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim RootDir As New DirectoryInfo("c:\") Dim RootNode As New TreeNode("c:\") treeFiles.Nodes.Add(RootNode) Fill(RootDir, RootNode) End Sub Private Sub treeFiles_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles treeFiles.BeforeExpand If e.Node.Nodes.Count > 0 Then If e.Node.FirstNode.Text = "*" Then e.Node.FirstNode.Remove() Dim DirectoryToFill As New DirectoryInfo(e.Node.FullPath) Fill(DirectoryToFill, e.Node) End If End If End Sub
End Class
</source>
Relational TreeView
<source lang="vbnet"> Imports System.Data.SqlClient Imports System.Windows.Forms <Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Class Relation
Inherits System.Windows.Forms.Form <System.Diagnostics.DebuggerStepThrough()> _ Private Sub InitializeComponent() Me.SplitContainer1 = New System.Windows.Forms.SplitContainer Me.treeDB = New System.Windows.Forms.TreeView Me.txtInfo = New System.Windows.Forms.TextBox Me.Panel1 = New System.Windows.Forms.Panel Me.cmdClear = New System.Windows.Forms.Button Me.cmdFill = New System.Windows.Forms.Button Me.SplitContainer1.Panel1.SuspendLayout() Me.SplitContainer1.Panel2.SuspendLayout() Me.SplitContainer1.SuspendLayout() Me.Panel1.SuspendLayout() Me.SuspendLayout() " "SplitContainer1 " Me.SplitContainer1.Dock = System.Windows.Forms.DockStyle.Fill Me.SplitContainer1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.SplitContainer1.Location = New System.Drawing.Point(5, 5) Me.SplitContainer1.Name = "SplitContainer1" " "SplitContainer1.Panel1 " Me.SplitContainer1.Panel1.Controls.Add(Me.treeDB) " "SplitContainer1.Panel2 " Me.SplitContainer1.Panel2.Controls.Add(Me.txtInfo) Me.SplitContainer1.Size = New System.Drawing.Size(460, 235) Me.SplitContainer1.SplitterDistance = 152 Me.SplitContainer1.TabIndex = 8 " "treeDB " Me.treeDB.Dock = System.Windows.Forms.DockStyle.Fill Me.treeDB.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.treeDB.Location = New System.Drawing.Point(0, 0) Me.treeDB.Name = "treeDB" Me.treeDB.Size = New System.Drawing.Size(152, 235) Me.treeDB.TabIndex = 6 " "txtInfo " Me.txtInfo.Dock = System.Windows.Forms.DockStyle.Fill Me.txtInfo.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.txtInfo.Location = New System.Drawing.Point(0, 0) Me.txtInfo.Multiline = True Me.txtInfo.Name = "txtInfo" Me.txtInfo.ReadOnly = True Me.txtInfo.Size = New System.Drawing.Size(304, 235) Me.txtInfo.TabIndex = 8 " "Panel1 " Me.Panel1.Controls.Add(Me.cmdClear) Me.Panel1.Controls.Add(Me.cmdFill) Me.Panel1.Dock = System.Windows.Forms.DockStyle.Bottom Me.Panel1.Font = New System.Drawing.Font("Tahoma", 8.25!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte)) Me.Panel1.Location = New System.Drawing.Point(5, 240) Me.Panel1.Name = "Panel1" Me.Panel1.Size = New System.Drawing.Size(460, 28) Me.Panel1.TabIndex = 7 " "cmdClear " Me.cmdClear.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles) Me.cmdClear.Location = New System.Drawing.Point(386, 4) Me.cmdClear.Name = "cmdClear" Me.cmdClear.Size = New System.Drawing.Size(72, 24) Me.cmdClear.TabIndex = 4 Me.cmdClear.Text = "Clear" " Me.cmdFill.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles) Me.cmdFill.Location = New System.Drawing.Point(4, 4) Me.cmdFill.Name = "cmdFill" Me.cmdFill.Size = New System.Drawing.Size(72, 24) Me.cmdFill.TabIndex = 3 Me.cmdFill.Text = "Fill" " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(470, 273) Me.Controls.Add(Me.SplitContainer1) Me.Controls.Add(Me.Panel1) Me.Name = "Relation" Me.Padding = New System.Windows.Forms.Padding(5) Me.Text = "Relation" Me.SplitContainer1.Panel1.ResumeLayout(False) Me.SplitContainer1.Panel2.ResumeLayout(False) Me.SplitContainer1.Panel2.PerformLayout() Me.SplitContainer1.ResumeLayout(False) Me.Panel1.ResumeLayout(False) Me.ResumeLayout(False) End Sub Friend WithEvents SplitContainer1 As System.Windows.Forms.SplitContainer Friend WithEvents treeDB As System.Windows.Forms.TreeView Friend WithEvents txtInfo As System.Windows.Forms.TextBox Friend WithEvents Panel1 As System.Windows.Forms.Panel Friend WithEvents cmdClear As System.Windows.Forms.Button Friend WithEvents cmdFill As System.Windows.Forms.Button Private Sub cmdFill_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdFill.Click Dim Connect As String = "Data Source=localhost;Integrated Security=True;Initial Catalog=Northwind;" Dim con As New SqlConnection(Connect) Dim SQL As String = "SELECT * FROM Orders WHERE OrderDate < "2000/01/01" AND OrderDate > "1987/01/01"" Dim cmd As New SqlCommand(SQL, con) Dim adapter As New SqlDataAdapter(cmd) Dim dsNorthwind As New DataSet() con.Open() adapter.Fill(dsNorthwind, "Orders") cmd.rumandText = "SELECT * FROM Customers" adapter.Fill(dsNorthwind, "Customers") cmd.rumandText = "SELECT * FROM Employees" adapter.Fill(dsNorthwind, "Employees") con.Close() Dim relCustomersOrders As New DataRelation("CustomersOrders", _ dsNorthwind.Tables("Customers").Columns("CustomerID"), _ dsNorthwind.Tables("Orders").Columns("CustomerID")) dsNorthwind.Relations.Add(relCustomersOrders) Dim nodeParent, nodeChild As TreeNode Dim rowParent, rowChild As DataRow For Each rowParent In dsNorthwind.Tables("Customers").Rows nodeParent = treeDB.Nodes.Add(rowParent("CompanyName")) nodeParent.Tag = rowParent For Each rowChild In rowParent.GetChildRows(relCustomersOrders) nodeChild = nodeParent.Nodes.Add(rowChild("OrderID")) nodeChild.Tag = rowChild Next Next End Sub Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click treeDB.Nodes.Clear() End Sub Private Sub treeDB_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles treeDB.AfterSelect txtInfo.Text = "" Dim row As DataRow = CType(e.Node.Tag, DataRow) Dim Field As Object For Each Field In row.ItemArray txtInfo.Text &= Field.ToString & vbNewLine Next End Sub
End Class
</source>
Tree Demo
<source lang="vbnet"> Imports System Imports System.Data Imports System.Windows.Forms Imports System.Drawing Imports System.Data.SqlClient public class MainClass
Shared Sub Main() Dim form1 As Form = New Form1 Application.Run(form1) End Sub
End Class
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim factory, group, person As TreeNode " Create some nodes. trvOrgChart.Nodes.Clear() factory = trvOrgChart.Nodes.Add( _ "f Name", "Name", 0, 0) group = factory.Nodes.Add( _ "g Dept 1", "Dept 2", 1, 1) person = group.Nodes.Add( _ "p Name 1", "Name 1", 2, 2) person = group.Nodes.Add( _ "p Name 2", "Name 2", 2, 2) group = factory.Nodes.Add( _ "g Dep 2", "Dep 2t", 1, 1) person = group.Nodes.Add( _ "p Name 1", "Name 1", 2, 2) person = group.Nodes.Add( _ "p Name 2", "Name 2", 2, 2) factory = trvOrgChart.Nodes.Add( _ "f Dept 3", "Dept 3", 0, 0) group = factory.Nodes.Add( _ "g Name 1", "Name 1", 1, 1) person = group.Nodes.Add( _ "p Name 2", "Name 2", 2, 2) person = group.Nodes.Add( _ "p Name 3", "Namr 3", 2, 2) group = factory.Nodes.Add( _ "g Dep 5", "Dep 5", 1, 1) person = group.Nodes.Add( _ "p Name 1", "Name 1", 2, 2) person = group.Nodes.Add( _ "p Name 2", "Namr 2", 2, 2) person = group.Nodes.Add( _ "p Name 3", "Name 3", 2, 2) group = factory.Nodes.Add( _ "g Dept 10", "Dept 10", 1, 1) person = group.Nodes.Add( _ "p Name 1", "Namr 1", 2, 2) person = group.Nodes.Add( _ "p Namr 2", "Name 2", 2, 2) trvOrgChart.ExpandAll() trvOrgChart.Nodes(0).EnsureVisible() End Sub Private Sub trvOrgChart_BeforeLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles trvOrgChart.BeforeLabelEdit e.CancelEdit = (e.Node.FullPath.IndexOf(trvOrgChart.PathSeparator) = -1) End Sub
End Class
<Global.Microsoft.VisualBasic.rupilerServices.DesignerGenerated()> _ Partial Public Class Form1
Inherits System.Windows.Forms.Form "Form overrides dispose to clean up the component list. <System.Diagnostics.DebuggerNonUserCode()> _ Protected Overloads 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() Dim TreeNode1 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("Cameron, Charlie", 2, 5) Dim TreeNode2 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("Davos, Debbie", 2, 5) Dim TreeNode3 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("Engineering", 1, 4, New System.Windows.Forms.TreeNode() {TreeNode1, TreeNode2}) Dim TreeNode4 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("Test", 1, 4) Dim TreeNode5 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("R & D", 0, 3, New System.Windows.Forms.TreeNode() {TreeNode3, TreeNode4}) Me.trvOrgChart = New System.Windows.Forms.TreeView Me.SuspendLayout() " "trvOrgChart " Me.trvOrgChart.Dock = System.Windows.Forms.DockStyle.Fill Me.trvOrgChart.ImageIndex = 0 Me.trvOrgChart.LabelEdit = True Me.trvOrgChart.Location = New System.Drawing.Point(0, 0) Me.trvOrgChart.Name = "trvOrgChart" TreeNode1.Name = "Node6" TreeNode1.SelectedImageIndex = 5 TreeNode1.Text = "Cameron, Charlie" TreeNode2.Name = "Node7" TreeNode2.SelectedImageIndex = 5 TreeNode2.Text = "Davos, Debbie" TreeNode3.ImageIndex = 1 TreeNode3.Name = "Node5" TreeNode3.SelectedImageIndex = 4 TreeNode3.Text = "Engineering" TreeNode4.ImageIndex = 1 TreeNode4.Name = "Node8" TreeNode4.SelectedImageIndex = 4 TreeNode4.Text = "Test" TreeNode5.ImageIndex = 0 TreeNode5.Name = "Node0" TreeNode5.SelectedImageIndex = 3 TreeNode5.Text = "R & D" Me.trvOrgChart.Nodes.AddRange(New System.Windows.Forms.TreeNode() {TreeNode5}) Me.trvOrgChart.SelectedImageIndex = 0 Me.trvOrgChart.Size = New System.Drawing.Size(292, 273) Me.trvOrgChart.TabIndex = 1 " "Form1 " Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!) Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.Add(Me.trvOrgChart) Me.Name = "Form1" Me.Text = "UseTreeView" Me.ResumeLayout(False) End Sub Friend WithEvents trvOrgChart As System.Windows.Forms.TreeView
End Class
</source>
TreeView: Resource Browser
<source lang="vbnet"> Imports System Imports System.ruponentModel Imports System.Windows.Forms Imports System.Data Imports System.Configuration Imports System.Drawing Imports System.Drawing.Drawing2D Imports System.Drawing.Text Imports System.Collections Imports System.IO " FileStream Imports System.Reflection " Assembly Imports System.Resources " Resource readers Public Class MainClass
Shared Sub Main() Dim myform As Form = New Form1() Application.Run(myform) 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 fileOpenMenuItem As System.Windows.Forms.MenuItem Friend WithEvents statusBar1 As System.Windows.Forms.StatusBar Friend WithEvents menuItem5 As System.Windows.Forms.MenuItem Friend WithEvents fileExitMenuItem As System.Windows.Forms.MenuItem Friend WithEvents openFileDialog1 As System.Windows.Forms.OpenFileDialog Friend WithEvents mainMenu1 As System.Windows.Forms.MainMenu Friend WithEvents menuItem1 As System.Windows.Forms.MenuItem Friend WithEvents menuItem3 As System.Windows.Forms.MenuItem Friend WithEvents resourcesTreeView As System.Windows.Forms.TreeView <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() Me.fileOpenMenuItem = New System.Windows.Forms.MenuItem() Me.statusBar1 = New System.Windows.Forms.StatusBar() Me.menuItem5 = New System.Windows.Forms.MenuItem() Me.fileExitMenuItem = New System.Windows.Forms.MenuItem() Me.openFileDialog1 = New System.Windows.Forms.OpenFileDialog() Me.mainMenu1 = New System.Windows.Forms.MainMenu() Me.menuItem1 = New System.Windows.Forms.MenuItem() Me.menuItem3 = New System.Windows.Forms.MenuItem() Me.resourcesTreeView = New System.Windows.Forms.TreeView() Me.SuspendLayout() " "fileOpenMenuItem " Me.fileOpenMenuItem.Index = 0 Me.fileOpenMenuItem.Shortcut = System.Windows.Forms.Shortcut.CtrlO Me.fileOpenMenuItem.Text = "&Open..." " "statusBar1 " Me.statusBar1.Location = New System.Drawing.Point(0, 192) Me.statusBar1.Name = "statusBar1" Me.statusBar1.Size = New System.Drawing.Size(472, 22) Me.statusBar1.TabIndex = 3 " "fileExitMenuItem " Me.fileExitMenuItem.Index = 2 Me.fileExitMenuItem.Text = "E&xit" " "openFileDialog1 " Me.openFileDialog1.Filter = "Resource Files (*.exe, *.dll, *.resx, *.resources)|*.exe;*.dll;*.resx;*.resources" & _ "|All Files (*.*)|*.*" " "mainMenu1 " Me.mainMenu1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.menuItem1, Me.menuItem5}) " "menuItem1 " Me.menuItem1.Index = 0 Me.menuItem1.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.fileOpenMenuItem, Me.menuItem3, Me.fileExitMenuItem}) Me.menuItem1.Text = "&File" " "menuItem3 " Me.menuItem3.Index = 1 Me.menuItem3.Text = "-" " "resourcesTreeView " Me.resourcesTreeView.Dock = System.Windows.Forms.DockStyle.Fill Me.resourcesTreeView.ImageIndex = -1 Me.resourcesTreeView.Name = "resourcesTreeView" Me.resourcesTreeView.SelectedImageIndex = -1 Me.resourcesTreeView.Size = New System.Drawing.Size(472, 214) Me.resourcesTreeView.Sorted = True Me.resourcesTreeView.TabIndex = 2 " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(472, 214) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.statusBar1, Me.resourcesTreeView}) Me.Menu = Me.mainMenu1 Me.Name = "Form1" Me.Text = "Resource Explorer" Me.ResumeLayout(False) End Sub
- End Region
Sub LoadResourcesFromFile(ByVal fileName As String) resourcesTreeView.Nodes.Clear() Dim root As TreeNode = resourcesTreeView.Nodes.Add(Path.GetFileName(fileName)) Select Case System.IO.Path.GetExtension(fileName).ToLower() Case ".exe", ".dll" LoadResourcesFromAssemblyFile(root, fileName) Case ".resx" LoadResourcesFromResxFile(root, fileName) Case ".resources" LoadResourcesFromResourcesFile(root, fileName) Case Else MessageBox.Show("Unknown file format") End Select root.Expand() End Sub Sub LoadResourcesFromAssemblyFile(ByVal parent As TreeNode, ByVal fileName As String) Dim assem As [Assembly] = [Assembly].LoadFrom(fileName) Dim resourceName As String For Each resourceName In assem.GetManifestResourceNames Dim node As TreeNode = parent.Nodes.Add(resourceName) If resourceName.ToLower().EndsWith(".resources") Then Dim s As Stream = assem.GetManifestResourceStream(resourceName) LoadResourcesFromResourcesStream(node, s) s.Close() End If Next End Sub Sub LoadResourcesFromResxFile(ByVal parent As TreeNode, ByVal fileName As String) Dim reader As ResXResourceReader = New ResXResourceReader(fileName) Dim entry As DictionaryEntry For Each entry In reader Dim node As TreeNode = parent.Nodes.Add(String.Format("{0} ({1})", entry.Key.ToString(), entry.Value.GetType())) node.Tag = entry.Value Next reader.Close() End Sub Sub LoadResourcesFromResourcesFile(ByVal parent As TreeNode, ByVal fileName As String) Dim s As FileStream = New FileStream(fileName, FileMode.Open, FileAccess.Read) LoadResourcesFromResourcesStream(parent, s) s.Close() End Sub Sub LoadResourcesFromResourcesStream(ByVal parent As TreeNode, ByVal s As Stream) Dim reader As ResourceReader = New ResourceReader(s) Dim entry As DictionaryEntry For Each entry In reader Dim node As TreeNode = parent.Nodes.Add(String.Format("{0} ({1})", entry.Key.ToString(), IIf(Not entry.Value Is Nothing, entry.Value.GetType().ToString(), "<none>"))) node.Tag = entry.Value Next reader.Close() End Sub Sub ShowSelectedNode() Dim node As TreeNode = resourcesTreeView.SelectedNode If node Is Nothing Then Exit Sub " TODO: Show the contents "MessageBox.Show(node.Text) End Sub Private Sub fileOpenMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fileOpenMenuItem.Click If openFileDialog1.ShowDialog = DialogResult.OK Then LoadResourcesFromFile(openFileDialog1.FileName) End If End Sub Private Sub fileExitMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles fileExitMenuItem.Click Me.Close() End Sub
Private Sub resourcesTreeView_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles resourcesTreeView.DoubleClick ShowSelectedNode() End Sub Private Sub resourcesTreeView_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles resourcesTreeView.KeyDown If Me Is Form.ActiveForm And (e.KeyCode And Keys.Enter = Keys.Enter) Then ShowSelectedNode() End Sub Private Sub resourcesTreeView_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles resourcesTreeView.AfterSelect statusBar1.Text = "" Dim node As TreeNode = resourcesTreeView.SelectedNode If node Is Nothing Then Exit Sub Dim value As Object = node.Tag If value Is Nothing Then Exit Sub statusBar1.Text = value.ToString() End Sub
End Class
</source>
Use Tree View to display directory
<source lang="vbnet"> Imports System.Windows.Forms Imports System.IO
Module Module1
Sub Main() Application.Run(New Form1) End Sub
End Module
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 Button1 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.SuspendLayout() " "TreeView1 " Me.TreeView1.ImageIndex = -1 Me.TreeView1.Location = New System.Drawing.Point(16, 16) Me.TreeView1.Name = "TreeView1" Me.TreeView1.SelectedImageIndex = -1 Me.TreeView1.Size = New System.Drawing.Size(256, 184) Me.TreeView1.TabIndex = 0 " "Button1 " Me.Button1.Location = New System.Drawing.Point(96, 224) Me.Button1.Name = "Button1" Me.Button1.Size = New System.Drawing.Size(96, 23) Me.Button1.TabIndex = 1 Me.Button1.Text = "View Directories" " "Form1 " Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13) Me.ClientSize = New System.Drawing.Size(292, 273) Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.TreeView1}) Me.Name = "Form1" Me.Text = "TreeViewDemo" Me.ResumeLayout(False) End Sub
- End Region
Sub ProcessTree(ByVal Dir As String) TreeView1.BeginUpdate() TreeView1.Nodes.Add(New TreeNode(Dir)) TreeView1.EndUpdate() TreeView1.Refresh() Dim DirObj As New DirectoryInfo(Dir) Dim Dirs As DirectoryInfo() = DirObj.GetDirectories("*.*") Dim DirectoryName As DirectoryInfo For Each DirectoryName In Dirs Try ProcessTree(DirectoryName.FullName) Catch E As Exception MessageBox.Show("Error accessing " & DirectoryName.FullName) End Try Next End Sub Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click ProcessTree("C:\") End Sub
End Class
</source>
Using a TreeView to display the directory structure
<source lang="vbnet"> Imports System Imports System.Drawing Imports System.Windows.Forms Imports System.IO Public Class MainClass
Shared Sub Main() Dim myform As Form = New FrmTreeViewDirectory() Application.Run(myform) End Sub " Main
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) Try 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 Catch unauthorized As UnauthorizedAccessException parentNode.Nodes.Add("Access Denied") End Try 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
</source>