directorytree.vb
来自「Samples are organized by chapter, and th」· VB 代码 · 共 174 行
VB
174 行
Imports System.IO
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.Windows.Forms.Design
Imports System.Drawing.Design
<LicenseProvider(GetType(LicFileLicenseProvider)), _
DefaultEvent("DirectorySelected"), _
DefaultProperty("Drive"), _
Designer(GetType(DirectoryTreeDesigner))> _
Public Class DirectoryTree
Inherits TreeView
Event DirectorySelected(ByVal sender As Object, ByVal e As DirectorySelectedEventArgs)
Private _Drive As Char
<Editor(GetType(DriveEditor), GetType(UITypeEditor))> _
Public Property Drive() As Char
Get
Return _Drive
End Get
Set(ByVal Value As Char)
_Drive = Value
'If Not Me.DesignMode Then
RefreshDisplay()
End Set
End Property
Private _License As License
Public Sub New()
'_License = LicenseManager.Validate(Me.GetType, Me)
End Sub
Public Function ShouldSerializeNodes() As Boolean
Return False
End Function
Public Sub RefreshDisplay()
' Erase the existing tree.
Me.Nodes.Clear()
' Set the first node.
Dim RootNode As New TreeNode(_Drive & ":\")
Me.Nodes.Add(RootNode)
' Fill the first level and expand it.
Fill(RootNode)
Me.Nodes(0).Expand()
End Sub
Private Sub Fill(ByVal DirNode As TreeNode)
Dim Dir As New DirectoryInfo(DirNode.FullPath)
Dim DirItem As DirectoryInfo
' An exception could be thrown in this code if you don't
' have sufficient security exceptions for a file or directory.
' You can catch and then ignore this exception.
For Each DirItem In Dir.GetDirectories
' Add node for the directory.
Dim NewNode As New TreeNode(DirItem.Name)
DirNode.Nodes.Add(NewNode)
NewNode.Nodes.Add("*")
Next
End Sub
Protected Overrides Sub OnBeforeExpand(ByVal e As TreeViewCancelEventArgs)
MyBase.OnBeforeExpand(e)
' If a dummy node is found, remove it and read the real directory list.
If e.Node.Nodes(0).Text = "*" Then
e.Node.Nodes.Clear()
Fill(e.Node)
End If
End Sub
Protected Overrides Sub OnAfterSelect(ByVal e As TreeViewEventArgs)
MyBase.OnAfterSelect(e)
' Raise the DirectorySelected event.
RaiseEvent DirectorySelected(Me, New DirectorySelectedEventArgs(e.Node.FullPath))
End Sub
Protected Overloads Overrides Sub Dispose(ByVal dispoing As Boolean)
If Not (_License Is Nothing) Then
_License.Dispose()
End If
MyBase.Dispose(Disposing)
End Sub
End Class
Public Class DirectorySelectedEventArgs
Inherits EventArgs
Public DirectoryName As String
Public Sub New(ByVal directoryName As String)
Me.DirectoryName = directoryName
End Sub
End Class
Public Class DirectoryTreeDesigner
Inherits ControlDesigner
Protected Overrides Sub PostFilterProperties(ByVal properties As System.Collections.IDictionary)
properties.Remove("Nodes")
End Sub
Private _Verbs As New DesignerVerbCollection()
Public Sub New()
_Verbs.Add(New DesignerVerb("Set Drive", New EventHandler(AddressOf OnVerb)))
End Sub
Public Overrides ReadOnly Property Verbs() As System.ComponentModel.Design.DesignerVerbCollection
Get
Return _Verbs
End Get
End Property
Protected Sub OnVerb(ByVal sender As Object, ByVal e As EventArgs)
' Show the form.
Dim frm As New SelectDrive()
frm.DriveSelection = CType(Me.Control, DirectoryTree).Drive
frm.ShowDialog()
' Adjust the associated control.
CType(Me.Control, DirectoryTree).Drive = frm.DriveSelection
' Notify the IDE that the Drive property has changed.
Dim Properties As PropertyDescriptorCollection = TypeDescriptor.GetProperties(GetType(DirectoryTree))
Dim ChangedProperty As PropertyDescriptor = Properties.Find("Drive", False)
Me.RaiseComponentChanged(ChangedProperty, "", frm.DriveSelection)
End Sub
End Class
Public Class DriveEditor
Inherits UITypeEditor
Public Overloads Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.Drawing.Design.UITypeEditorEditStyle
' We will use a window for property editing.
Return UITypeEditorEditStyle.Modal
End Function
Public Overloads Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
Dim frm As New SelectDrive()
' Set current drive in window.
frm.DriveSelection = value
frm.ShowDialog()
' Return the new value.
Return frm.DriveSelection
End Function
Public Overloads Overrides Function GetPaintValueSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
' No special thumbnail will be shown for the grid.
Return False
End Function
End Class
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?