TreeViewを使用して配置ツールを作成しようとしています。私は、フォルダ/サブフォルダとファイルでツリービューを作成するためにオンラインで見つけたチュートリアルのいくつかを続けました。ファイルの展開を処理するためのすべての作業と機能は問題ないようですが、私は表示上の問題があります。VB.Net TreeViewでノードを更新する方法NodeMouseClick
My TreeViewは、各フォルダ/ファイルに正しいアイコンイメージを添付しても、自分のフォルダ構造と各フォルダ内のファイルを適切に表示します。
ノード(フォルダ)を展開または折りたたんでも問題はありませんが、フォルダを1回クリックすると、_NodeMouseClickイベントが発生し、内容が正しく更新されません。すべてのサブフォルダは表示されなくなり、ファイルにはフォルダアイコンが追加されます。フォルダノードを折りたたんで再展開すると、すべてが元の状態に戻ります。
はここに関連するコードです:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
' when our component is loaded, we initialize the TreeView by adding the root node
Dim mRootNode As New TreeNode
mRootNode.Text = RootPath
mRootNode.Tag = RootPath
mRootNode.ImageKey = CacheShellIcon(RootPath)
mRootNode.SelectedImageKey = mRootNode.ImageKey
mRootNode.Nodes.Add("*DUMMY*")
TreeView1.Nodes.Add(mRootNode)
End Sub
Private Sub _BeforeCollapse(sender As Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeCollapse
' clear the node that is being collapsed
e.Node.Nodes.Clear()
' and add a dummy TreeNode to the node being collapsed so it is expandable again
e.Node.Nodes.Add("*DUMMY*")
End Sub
Private Sub _BeforeExpand(sender As Object, e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
' clear the expanding node so we can re-populate it, or else we end up with duplicate nodes
e.Node.Nodes.Clear()
AddImages(e)
End Sub
Private Sub _AfterSelect(sender As System.Object, e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
e.Node.Nodes.Clear()
Dim folder As String = CStr(e.Node.Tag)
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))
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
私は私がしようと_NodeMouseClickルーチンからAddImagesルーチンを呼び出す必要があると思うが、それを動作させることができていません。 AddImagesはTreeViewCancelEventArgsを受け入れ、_NodeMouseClickルーチンにはありません。
Private Sub AddImages(ByRef e As System.Windows.Forms.TreeViewCancelEventArgs)
'---[ get the directory representing this node ]---
Dim mNodeDirectory = New IO.DirectoryInfo(e.Node.Tag.ToString)
'---[ add each subdirectory from the file system to the expanding node as a child node ]---
For Each mDirectory As IO.DirectoryInfo In mNodeDirectory.GetDirectories
'---[ declare a child TreeNode for the next subdirectory ]---
Dim mDirectoryNode As New TreeNode
'---[ store the full path to this directory in the child TreeNode's Tag property ]---
mDirectoryNode.Tag = mDirectory.FullName
'---[ set the child TreeNodes's display text ]---
mDirectoryNode.Text = mDirectory.Name
mDirectoryNode.ImageKey = CacheShellIcon(mDirectory.FullName)
mDirectoryNode.SelectedImageKey = mDirectoryNode.ImageKey
'---[ add a dummy TreeNode to this child TreeNode to make it expandable ]---
mDirectoryNode.Nodes.Add("*DUMMY*")
'---[ add this child TreeNode to the expanding TreeNode ]---
e.Node.Nodes.Add(mDirectoryNode)
Next
'---[ add each file from the file system that is a child of the argNode that was passed in ]---
For Each mFile As IO.FileInfo In mNodeDirectory.GetFiles
'---[ declare a TreeNode for this file ]---
Dim mFileNode As New TreeNode
'---[ store the full path to this file in the file TreeNode's Tag property ]---
mFileNode.Tag = mFile.FullName
'---[ set the file TreeNodes's display text ]---
mFileNode.Text = mFile.Name
mFileNode.ImageKey = CacheShellIcon(mFile.FullName)
mFileNode.SelectedImageKey = mFileNode.ImageKey & "-open"
'---[ add this file TreeNode to the TreeNode that is being populated ]---
e.Node.Nodes.Add(mFileNode)
Next
End Sub
もし誰かがヒントを持っていれば、私は非常に助けていただければ幸いです。 おかげで、
Genius!私はAfterSelectイベントを見ることさえ考えなかった!そのコードは今どこにでも置かれていて、私がコメントしたら、問題は解消されました。どうもありがとうございます! – tkflick