[RESOLVED] [2003] Rename File / Folder on Treeview AfterLabelEdit
Hello everyone.
I have a treeview with it's LabelEdit property set to true. This treeview shows all the folders, with all it's files and subfolders inside.
I want to know how can I rename a file or folder, from the treeview. I presume I need either the BeforeLabelEdit event or the AfterLabelEdit event, but I have no clue how to do this.
Can anyone help?
Re: [2003] Rename File / Folder on Treeview AfterLabelEdit
If you store the node info (such as fullpath, type (drive, folder or file)) to the Tag property of the treenode, you can later read back the needed information of the node to do the rename.
This sample code is in 2005, but you get the idea... For 2003, you don't have My.Computer object, so you just have to use System.IO.Directory.Move(source, dest) and System.IO.File.Move(source, dest) to rename.
Code:
Public Class NodeInfo
Private _FullPath As String
Private _Type As Integer
' Type 0: drive
' Type 1: folder
' Type 2: file
Public Sub New(ByVal nodeFullPath As String, ByVal nodeType As Integer)
_FullPath = nodeFullPath
_Type = nodeType
End Sub
Public Property FullPath() As String
Get
Return _FullPath
End Get
Set(ByVal value As String)
_FullPath = value
End Set
End Property
Public Property Type() As Integer
Get
Return _Type
End Get
Set(ByVal value As Integer)
_Type = value
End Set
End Property
End Class
Private Sub TreeView1_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles TreeView1.AfterLabelEdit
Dim info As NodeInfo = DirectCast(e.Node.Tag, NodeInfo)
Dim dir As String = System.IO.Path.GetDirectoryName(info.FullPath)
Dim txt As String = e.Label
'You probably want to text is valid for folder and file names
'If it contains any invalid character, you set e.CancelEdit= True to revert the changes
'Now you determine what type of node this is
Select Case info.Type
Case 0 'It's a drive... Cancel the edit
e.CancelEdit = True
Case 1 'it's a folder
My.Computer.FileSystem.RenameDirectory(info.FullPath, txt)
Case 2 'it's a file
My.Computer.FileSystem.RenameFile(info.FullPath, txt)
End Select
'And finally you update the node info
Dim newName As String = IO.Path.Combine(dir, txt)
info.FullPath = newName
End Sub
Re: [2003] Rename File / Folder on Treeview AfterLabelEdit
Thanx for your input sir.
say for example I click on a folder how do i know it is a folder, how do i know it's a file, because it seems I cannot just use one thing to rename I must use either File.Move or Directory.Move. So I guess, itbegs the question, how do i know that the selected node / subnode is a file or folder - how do i determine that?
Re: [2003] Rename File / Folder on Treeview AfterLabelEdit
Quote:
Originally Posted by GrimmReaper
Thanx for your input sir.
say for example I click on a folder how do i know it is a folder, how do i know it's a file, because it seems I cannot just use one thing to rename I must use either File.Move or Directory.Move. So I guess, itbegs the question, how do i know that the selected node / subnode is a file or folder - how do i determine that?
That's why I said that you need to store the info on the node. In my example, I create a NodeInfo class that has 2 public properties: FullPath and Type. So when you create a new treenode and to add to the treeview, you also need to create a new NodeInfo object and assign it to that new treenode's Tag property.
Re: [2003] Rename File / Folder on Treeview AfterLabelEdit
Wow, that is some great code, I think i understand what it means. i do however get a small error, on this line:
Code:
Dim info As NodeInfo = DirectCast(e.Node.Tag, NodeInfo)
it tells me that the specified cast is not valid
Re: [2003] Rename File / Folder on Treeview AfterLabelEdit
Did you store a NodeInfo object in the Tag property of the treenode? Can you show the code where you populate the treeview?
Re: [2003] Rename File / Folder on Treeview AfterLabelEdit
Thanx for all your patience sir.
This is how i populate my treeview:
Code:
Private Sub AddAllFolders(ByVal node As TreeNode, ByVal path As String)
Try
For Each FolderNode As String In Directory.GetDirectories(path)
Dim SubFolderNode As TreeNode = node.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))
SubFolderNode.Tag = FolderNode
SubFolderNode.Nodes.Add("Loading...")
Dim info As New NodeInfo(SubFolderNode.Tag, 1)
' info.FullPath = SubFolderNode.Tag
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub frmLocal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Tnode As TreeNode = trvLocal.Nodes.Add("C:")
AddAllFolders(Tnode, "C:\")
' trvLocal.Sort()
End Sub
Private Sub trvLocal_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles trvLocal.AfterSelect
If trvLocal.SelectedNode.Nodes.Count = 1 AndAlso trvLocal.SelectedNode.Nodes(0).Text = "Loading..." Then
trvLocal.SelectedNode.Nodes.Clear()
AddAllFolders(trvLocal.SelectedNode, CStr(trvLocal.SelectedNode.Tag))
End If
Dim folder As String = CStr(e.Node.Tag)
strLocalSelFolder = CStr(e.Node.Tag)
' Dim info As NodeInfo = DirectCast(e.Node.Tag, NodeInfo)
If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
Try
For Each file As String In IO.Directory.GetFiles(folder)
e.Node.Nodes.Add(file.Substring(file.LastIndexOf("\"c) + 1))
Dim info As New NodeInfo(file, 2)
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
strLocalSelFile = e.Node.FullPath
End Sub
Private Sub trvLocal_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles trvLocal.BeforeExpand
If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then
e.Node.Nodes.Clear()
AddAllFolders(e.Node, CStr(e.Node.Tag))
End If
End Sub
And this is my AfterEdit event:
Code:
Private Sub trvLocal_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles trvLocal.AfterLabelEdit
Dim info As NodeInfo = DirectCast(CStr(e.Node.Tag), NodeInfo)
Dim dir As String = System.IO.Path.GetDirectoryName(info.FullPath)
Dim txt As String = e.Label
'You probably want to text is valid for folder and file names
'If it contains any invalid character, you set e.CancelEdit= True to revert the changes
'Now you determine what type of node this is
Select Case info.Type
Case 0 'It's a drive... Cancel the edit
e.CancelEdit = True
Case 1 'it's a folder
Directory.Move(info.FullPath, "FolderTest")
' My.Computer.FileSystem.RenameDirectory(info.FullPath, txt)
Case 2 'it's a file
File.Move(info.FullPath, "FileTest")
'My.Computer.FileSystem.RenameFile(info.FullPath, txt)
End Select
'And finally you update the node info
Dim newName As String = IO.Path.Combine(dir, txt)
info.FullPath = newName
End Sub
Have i done something wrong?
Re: [2003] Rename File / Folder on Treeview AfterLabelEdit
Your code is a mess :)
Try this:
Code:
Public Class NodeInfo
Private _FullPath As String = String.Empty
Private _Type As Integer = -1
Public Sub New(ByVal fullPath As String, ByVal type As Integer)
_FullPath = fullPath
_Type = type
End Sub
Public Property FullPath() As String
Get
Return _FullPath
End Get
Set(ByVal value As String)
_FullPath = value
End Set
End Property
Public ReadOnly Property Type() As Integer
Get
Return _Type
End Get
End Property
End Class
Private Sub frmLocal_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.LoadTreeView()
End Sub
Private Sub LoadTreeView()
Me.trvLocal.BeginUpdate()
Dim root As TreeNode = Me.trvLocal.Nodes.Add("C:")
With root
.Tag = New NodeInfo("C:\", 0)
'Add a dummy node so that we can load children nodes on demand
.Nodes.Add("Loading...")
.Collapse(True)
End With
Me.trvLocal.EndUpdate()
End Sub
Private Sub LoadTreeNodes(ByVal parent As TreeNode)
Dim info As NodeInfo = DirectCast(parent.Tag, NodeInfo)
Me.trvLocal.BeginUpdate()
Dim folder As String = String.Empty
Dim file As String = string.Empty
Try
'Add folders to treeview
Dim folders() As String = IO.Directory.GetDirectories(info.FullPath)
If folders.Length <> 0 Then
Dim folderNode As TreeNode = Nothing
Dim folderName As String = String.Empty
For Each folder In folders
folderName = IO.Path.GetFileName(folder)
folderNode = parent.Nodes.Add(folderName)
With folderNode
.Tag = New NodeInfo(folder, 1)
.Nodes.Add("Loading...")
.Collapse(True)
End With
Next
End If
'Add files to treeview
Dim files() As String = IO.Directory.GetFiles(info.FullPath)
For Each file In files
Dim fileNode As TreeNode = parent.Nodes.Add(IO.Path.GetFileName(file))
fileNode.Tag = New NodeInfo(file, 2)
Next
Catch ex As UnauthorizedAccessException
Dim deniedNode As TreeNode = parent.Nodes.Add("Access Denied")
deniedNode.Tag = New NodeInfo(String.Empty, 1)
End Try
Me.trvLocal.EndUpdate()
End Sub
Private Sub trvLocal_BeforeExpand(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles trvLocal.BeforeExpand
If e.Action = TreeViewAction.Expand Then
Dim parent As TreeNode = e.Node
Dim firstNode As TreeNode = parent.FirstNode
If firstNode IsNot Nothing AndAlso firstNode.Text = "Loading..." Then
parent.Nodes.Clear()
Me.LoadTreeNodes(parent)
End If
End If
End Sub
Private Sub trvLocal_AfterLabelEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.NodeLabelEditEventArgs) Handles trvLocal.AfterLabelEdit
Dim info As NodeInfo = DirectCast(e.Node.Tag, NodeInfo)
Dim dir As String = System.IO.Path.GetDirectoryName(info.FullPath)
Dim txt As String = e.Label
If IsValidFileOrFolderName(txt) Then
'Now you determine what type of node this is
Dim newName As String = IO.Path.Combine(dir, txt)
Select Case info.Type
Case 0 'It's a drive... Cancel the edit
e.CancelEdit = True
Case 1 'it's a folder
IO.Directory.Move(info.FullPath, newName)
Case 2 'it's a file
IO.File.Move(info.FullPath, newName)
End Select
'And finally you update the node info
info.FullPath = newName
Else
e.CancelEdit = True
End If
End Sub
Private Function IsValidFileOrFolderName(ByVal fileorFolderName As String) As Boolean
'Put your own logic here to check for any invalid characters in the file/folder name.
'If presence, return false. Else return true
Return True
End Function
Re: [2003] Rename File / Folder on Treeview AfterLabelEdit
You are a GENIUS!!
Wow, thank you very very much kind sir! I tried rating you, but it keeps saying i must spread some around :(