📄 folderbrowserform.vb
字号:
'
' FolderBrowserForm
' Underlying implementation of the FolderBrowserDialog
' (c) 2004 Peter Foot, OpenNETCF
'
Imports System
Imports System.Drawing
Imports System.IO
Imports System.Windows.Forms
Namespace OpenNETCF.Windows.Forms
'/ <summary>
'/ Presents a dialog allowing the user to browse the file system on the device.
'/ </summary>
Friend Class FolderBrowserForm
Inherits Form
Private mainMenu1 As System.Windows.Forms.MainMenu
Private ilIcons As System.Windows.Forms.ImageList
Private WithEvents tvFolders As System.Windows.Forms.TreeView
Private WithEvents mnuDone As System.Windows.Forms.MenuItem
Private WithEvents mnuCancel As System.Windows.Forms.MenuItem
Private mnuMenu As System.Windows.Forms.MenuItem
Private WithEvents mnuNew As System.Windows.Forms.MenuItem
Private m_selectedpath As String
Private Sub InitializeComponent()
Me.tvFolders = New System.Windows.Forms.TreeView
Me.ilIcons = New System.Windows.Forms.ImageList
Me.mainMenu1 = New System.Windows.Forms.MainMenu
Me.mnuDone = New System.Windows.Forms.MenuItem
Me.mnuMenu = New System.Windows.Forms.MenuItem
Me.mnuNew = New System.Windows.Forms.MenuItem
Me.mnuCancel = New System.Windows.Forms.MenuItem
'
' tvFolders
'
Me.tvFolders.ImageList = Me.ilIcons
Me.tvFolders.Location = New System.Drawing.Point(8, 32)
Me.tvFolders.ShowRootLines = False
Me.tvFolders.Size = New System.Drawing.Size(152, 112)
'
' ilIcons
'
Me.ilIcons.ImageSize = New System.Drawing.Size(16, 16)
'
' mainMenu1
'
Me.mainMenu1.MenuItems.Add(Me.mnuDone)
Me.mainMenu1.MenuItems.Add(Me.mnuMenu)
'
' mnuDone
'
Me.mnuDone.Text = "Select"
'
' mnuMenu
'
Me.mnuMenu.MenuItems.Add(Me.mnuNew)
Me.mnuMenu.MenuItems.Add(Me.mnuCancel)
Me.mnuMenu.Text = "Menu"
'
' mnuNew
'
Me.mnuNew.Enabled = False
Me.mnuNew.Text = "New Folder"
'
' mnuCancel
'
Me.mnuCancel.Text = "Cancel"
'
' InnerFolderBrowserDialog
'
Me.ClientSize = New System.Drawing.Size(170, 175)
Me.ControlBox = False
Me.Controls.Add(tvFolders)
Me.MaximizeBox = False
Me.Menu = Me.mainMenu1
Me.MinimizeBox = False
Me.Text = "Browse for Folder"
End Sub 'InitializeComponent
'/ <summary>
'/ Create a new instance of the FolderBrowserDialog
'/ </summary>
Public Sub New()
'
' TODO: Add constructor logic here
'
InitializeComponent()
'add folder images to imagelist
ilIcons.Images.Add(New System.Drawing.Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SmartphoneDialogs.Folder.gif")))
ilIcons.Images.Add(New System.Drawing.Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SmartphoneDialogs.Device.gif")))
ilIcons.Images.Add(New System.Drawing.Bitmap(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("SmartphoneDialogs.Card.gif")))
'set default image index to 0
tvFolders.ImageIndex = 0
tvFolders.SelectedImageIndex = 0
'use a backslash between path levels
tvFolders.PathSeparator = "\"
'setup default tree
Reset()
End Sub 'New
#Region "Public Properties"
'/ <summary>
'/ Gets or sets the path selected by the user.
'/ </summary>
Public Property SelectedPath() As String
Get
Return m_selectedpath
End Get
Set(ByVal Value As String)
'check that value passed is a valid file system path
If Directory.Exists(Value) Then
m_selectedpath = Value
'split the path into each folder layer
Dim layers As String() = Value.Split("\"c)
'mark the current node
Dim tn As TreeNode = tvFolders.Nodes(0)
'loop through the folder levels
Dim folderlevel As String
For Each folderlevel In layers
'ignore blank (top-level node)
If folderlevel <> "" Then
'add sub-folders
AddFolders(tn)
'find the required subfolder
Dim thisnode As TreeNode
For Each thisnode In tn.Nodes
'check node text
If thisnode.Text = folderlevel Then
'if it does set it and continue processing the next level
tn = thisnode
Exit For
End If
Next thisnode
End If
Next folderlevel
'select the final node
tvFolders.SelectedNode = tn
End If
End Set
End Property
'/ <summary>
'/ Gets or sets a value indicating whether the New Folder item appears in the menu.
'/ </summary>
Public Property ShowNewFolderButton() As Boolean
Get
Return mnuNew.Enabled
End Get
Set(ByVal Value As Boolean)
mnuNew.Enabled = Value
End Set
End Property
#End Region
#Region "Helper Functions"
'/ <summary>
'/ Adds the root node for the device to the TreeView
'/ </summary>
Private Sub AddRoot()
'stop updates during filling
tvFolders.BeginUpdate()
'add an empty node (use device image)
Dim root As TreeNode = tvFolders.Nodes.Add("")
root.ImageIndex = 1
root.SelectedImageIndex = 1
tvFolders.EndUpdate()
End Sub 'AddRoot
'/ <summary>
'/ Adds subfolders to a specified node in the tree
'/ </summary>
'/ <param name="tn">Node to add sub-folders to</param>
Private Sub AddFolders(ByVal tn As TreeNode)
'stop updates during filling
tvFolders.BeginUpdate()
'path to query for subfolders
Dim path As String
If tn.FullPath = "" Then
'for blank path (root) substitute '\'
path = "\"
Else
'use the nodes full path
path = tn.FullPath
End If
'clear existing subnodes if present
tn.Nodes.Clear()
'get all folders beneath the selected node
Dim directory As String
For Each directory In System.IO.Directory.GetDirectories(path)
Dim newnode As New TreeNode
'format human friendly name
newnode.Text = directory.Substring(directory.LastIndexOf("\") + 1, directory.Length - directory.LastIndexOf("\") - 1)
'change icon if folder is a storage card
Dim di As New DirectoryInfo(directory)
If (di.Attributes And FileAttributes.Temporary) = FileAttributes.Temporary Then
newnode.ImageIndex = 2
newnode.SelectedImageIndex = 2
End If
'add to root of tree
tn.Nodes.Add(newnode)
Next directory
'restore events etc
tvFolders.EndUpdate()
End Sub 'AddFolders
'/ <summary>
'/ Resets properties to their default values.
'/ </summary>
Public Sub Reset()
'restores dialog to initial settings and repopulates folders
Me.m_selectedpath = ""
tvFolders.Nodes.Clear()
'add the root (device) node to the tree
AddRoot()
'add root level subfolders
AddFolders(tvFolders.Nodes(0))
'expand the top level folders
tvFolders.Nodes(0).Expand()
End Sub 'Reset
#End Region
#Region "Event Handlers"
'/ <summary>
'/ Handles dynamic sizing of the dialog contents to the available screen space
'/ </summary>
'/ <param name="sender"></param>
'/ <param name="e"></param>
Private Sub FolderBrowserDialog_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Resize
'set the control to fill the available screen space
tvFolders.Bounds = New Rectangle(-1, -1, Screen.PrimaryScreen.WorkingArea.Width + 2, Screen.PrimaryScreen.WorkingArea.Height + 2)
End Sub 'FolderBrowserDialog_Resize
'/ <summary>
'/ Handles selection of a tree node
'/ </summary>
'/ <param name="sender">Sender of the event (in this case always tvFolders)</param>
'/ <param name="e">Describes the event.</param>
Private Sub tvFolders_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFolders.AfterSelect
'set the currently selected folder
If tvFolders.SelectedNode.FullPath = "" Then
'the root node is returned from the tree as "" but for the filsystem we need this to be "\"
m_selectedpath = "\"
Else
'for all other folders the fullpath directly matches the filesystem path
m_selectedpath = tvFolders.SelectedNode.FullPath
End If
'if there are no child nodes we will check for any and add them
If tvFolders.SelectedNode.Nodes.Count = 0 Then
AddFolders(tvFolders.SelectedNode)
End If
End Sub 'tvFolders_AfterSelect
Private Sub mnuDone_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuDone.Click
'user made a selection return OK
Me.DialogResult = DialogResult.OK
End Sub 'mnuDone_Click
Private Sub mnuCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuCancel.Click
'user aborted selection
Me.DialogResult = DialogResult.Cancel
End Sub 'mnuCancel_Click
Private Sub mnuNew_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles mnuNew.Click
'get a name
Dim foldername As String = Microsoft.VisualBasic.Interaction.InputBox("Enter new folder name", "Create Folder", "New Folder", 0, 0)
'if user cancelled take no further action
If foldername <> "" Then
'try to create a folder
Try
'call IO function to create the specified directory
Dim newpath As String = tvFolders.SelectedNode.FullPath + "\" + foldername
Directory.CreateDirectory(newpath)
'refresh tree
Dim newnode As TreeNode = tvFolders.SelectedNode.Nodes.Add(foldername)
'select new folder
tvFolders.SelectedNode = newnode
Catch
'warn the user
MessageBox.Show("Error creating folder", "Create Folder", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
Return
End Try
End If
End Sub 'mnuNew_Click
#End Region
End Class 'FolderBrowserForm
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -