|
■No79840 (バイク さん) に返信
> Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
これだと、同じフォルダーを選択するたびに
子フォルダーが重複追加されてしまいますよ。
> 親フォルダでチェックボックスをクリックしたら、
> 子フォルダや孫フォルダ等にチェックが入るようにしたいと
> 思っております
AfterCheck イベントを使うとか。ダブルクリック時に注意。
Imports System.IO
Public Class Form1
'ノードがチェックされたら、子をチェックする
Private Sub TreeView1_AfterCheck(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterCheck
Dim b As Boolean = e.Node.Checked
For Each n As TreeNode In e.Node.Nodes
'イベントの連鎖により、孫以降の子孫に対しても処理される
n.Checked = b
Next
End Sub
Private Sub SetChildren(baseNode As TreeNode)
For Each subDir As String In Directory.GetDirectories(baseNode.FullPath)
Dim n As TreeNode = baseNode.Nodes.Add(Path.GetFileName(subDir))
n.Checked = baseNode.Checked
Try
If Directory.GetDirectories(subDir).Any() Then
n.Nodes.Add("NUL") 'サブフォルダがある場合に[+]を付与するためのダミー
End If
Catch ex As Exception
End Try
Next
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
treeViewSubClass = New IgnoreDoubleClickSubClass()
treeViewSubClass.AssignHandle(TreeView1.Handle)
TreeView1.CheckBoxes = True
Dim rootNode As TreeNode = TreeView1.Nodes.Add("C:", "C:\")
SetChildren(rootNode)
rootNode.Expand()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
treeViewSubClass.ReleaseHandle()
treeViewSubClass = Nothing
End Sub
Private Sub TreeView1_BeforeExpand(sender As Object, e As TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
If e.Node.Nodes(0).Text = "NUL" Then
e.Node.Nodes.RemoveAt(0)
SetChildren(e.Node)
End If
End Sub
#Region "ダブルクリック対策(KB2781622)"
Private Class IgnoreDoubleClickSubClass
Inherits NativeWindow
Protected Overrides Sub WndProc(ByRef m As Message)
Const WM_LBUTTONDBLCLK As Integer = &H203
If m.Msg = WM_LBUTTONDBLCLK Then
m.Result = IntPtr.Zero
Else
MyBase.WndProc(m)
End If
End Sub
End Class
Private treeViewSubClass As IgnoreDoubleClickSubClass
#End Region
End Class
|