|
-
May 13th, 2011, 03:34 AM
#1
Thread Starter
New Member
[ask] TreeView with checkbox
Hi, i'm the new one on vbforums.
sorry for my english.. 
ehm, i have a problems with my application..
1. collapse and expand treeview
when i click plus sign (+) on treeview, treeview didn't expand the subfolder
but when i click image on treeview, then expand it.
2. showing path that i check on treeview
when i check the treeview then i click it the button, will return path that i check
this is my code..
Code:
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure SHFILEINFO
Public hIcon As IntPtr
Public iIcon As Integer
Public dwAttributes As Integer
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
(ByVal pszPath As String, ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0
Friend nIndex As Long = 0
Dim FileExtension As String
Dim SubItemIndex As Integer
Dim DateMod As String
Dim hImgSmall As IntPtr = Nothing
Dim hImgLarge As IntPtr = Nothing
Dim shinfo As SHFILEINFO
Dim n As TreeNode
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TreeView1.ExpandAll()
TreeView1.ImageList = ImageList1
shinfo.szDisplayName = New String(Chr(0), 260)
shinfo.szTypeName = New String(Chr(0), 80)
Dim Dnode As String = Nothing
Dim oDrive As IO.DriveInfo
For Each oDrive In IO.DriveInfo.GetDrives()
If oDrive.IsReady = False Then
Dnode = oDrive.Name
GetIcon(Dnode)
Dim Tnode As TreeNode = TreeView1.Nodes.Add(Dnode)
Tnode.ImageIndex = nIndex + 2 'The first two images (index 0 & 1) are for closed and open folder images already added.
Tnode.SelectedImageIndex = nIndex + 2
Else
Dnode = oDrive.Name
GetIcon(Dnode)
Dim Tnode As TreeNode = TreeView1.Nodes.Add(Dnode)
Tnode.ImageIndex = nIndex + 2
Tnode.SelectedImageIndex = nIndex + 2
GetFolders(Tnode, Dnode)
End If
nIndex += 1
Next
End Sub
Private Sub GetFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
Dim nFldNode As String
Try
For Each FolderNode As String In Directory.GetDirectories(FolderPath)
nFldNode = FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1)
Dim SubFolderNode As TreeNode = TNode.Nodes.Add(nFldNode)
SubFolderNode.Tag = FolderNode
SubFolderNode.Nodes.Add("...")
SubFolderNode.ImageIndex = 0
SubFolderNode.SelectedImageIndex = 1
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Treeview1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
If TreeView1.SelectedNode.Nodes.Count = 1 AndAlso TreeView1.SelectedNode.Nodes(0).Text = "..." Then
TreeView1.SelectedNode.Nodes.Clear()
GetFolders(TreeView1.SelectedNode, CStr(TreeView1.SelectedNode.Tag))
End If
End Sub
Private Sub Treeview1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "..." Then
e.Node.Nodes.Clear()
End If
End Sub
Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick
TreeCheckBoxes(TreeView1, e.Node)
End Sub
Public Sub TreeCheckBoxes(ByVal TR As TreeView, ByVal CurrentNode As TreeNode)
'Desarrollado por Jimmy Poma (lima -Peru) fecha 26-Abril-2009
'Si tiene algun cambio me lo hacen saber enviando el cambio a [email protected]
'Procedimiento que asigna check a los hijos y padres de un treeView
Dim lParentNode As TreeNode
Dim EstadoCheck As Boolean
Static noEntraNode As Boolean
If noEntraNode Then Exit Sub
noEntraNode = True
EstadoCheck = CurrentNode.Checked
If CurrentNode.Checked = True Then 'Node con Check
'da Check a los Hijos
Checkhijos(CurrentNode, EstadoCheck)
'Pone el Chek as los padres
lParentNode = CurrentNode
Do While Not lParentNode.Parent Is Nothing
lParentNode.Checked = EstadoCheck
lParentNode = lParentNode.Parent
Loop
lParentNode.Checked = EstadoCheck
Else
'Quita el check a los hijos
Checkhijos(CurrentNode, EstadoCheck)
'verifica si algun hijo tiene check sino quita el check los padres
CheckPadre(CurrentNode, EstadoCheck)
End If
noEntraNode = False
End Sub
Private Sub Checkhijos(ByVal Node As TreeNode, ByVal bCheck As Boolean)
'Desarrollado por Jimmy Poma (lima -Peru) fecha 26-Abril-2009
'Si tiene algun cambio me lo hacen saber enviando el cambio a [email protected]
'Procedimiento que asigna check a los hijos de un treeView
Dim i As Short
Dim nodX As TreeNode
If Node.GetNodeCount(False) <> 0 Then
nodX = Node.FirstNode
For i = 0 To Node.GetNodeCount(False) - 1
nodX.Checked = bCheck
Checkhijos(nodX, bCheck)
nodX = nodX.NextNode
Next
End If
End Sub
Private Sub CheckPadre(ByVal Node As TreeNode, ByVal bCheck As Boolean)
'Desarrollado por Jimmy Poma (lima -Peru) fecha 26-Abril-2009
'Si tiene algun cambio me lo hacen saber enviando el cambio a [email protected]
'Procedimiento que asigna check a los hijos de un treeView
Dim i As Short
Dim nodX As TreeNode
If Node.Parent Is Nothing Then
Node.Checked = bCheck
Exit Sub
End If
Debug.Print(Node.Parent.GetNodeCount(False))
If Node.Parent.GetNodeCount(False) = 1 Then
nodX = Node.Parent
nodX.Checked = bCheck
CheckPadre(nodX, bCheck)
End If
If Node.Parent.GetNodeCount(False) > 1 Then
Dim NodeHermano As TreeNode
Dim QuitaNode As Boolean
'Debug.Print("Node Inicail - " & Node.Text)
'Debug.Print("Node Node.Parent - " & Node.Parent.Text)
'Debug.Print("Node Node.Parent.FirstNode - " & Node.Parent.FirstNode.Text)
If Not Node.Parent.FirstNode Is Nothing Then
NodeHermano = Node.Parent.FirstNode
End If
'Debug.Print("Node Hermano 1 " & NodeHermano.Text)
'Debug.Print(NodeHermano.Parent.GetNodeCount(False))
QuitaNode = True
For i = 0 To NodeHermano.Parent.GetNodeCount(False) - 1
Debug.Print(NodeHermano.Text)
If NodeHermano.Checked Then
If Node.Level = NodeHermano.Level And Node.Index <> NodeHermano.Index Then
QuitaNode = False
Exit For
End If
End If
NodeHermano = NodeHermano.NextNode
Next
If QuitaNode Then
Node.Parent.Checked = bCheck
CheckPadre(Node.Parent, bCheck)
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' Would you help me?
' to knowing that folders/subfolders that i checked on treeview?
End Sub
Private Sub CheckForCheckedChildren(ByVal sender As Object, ByVal e As TreeViewCancelEventArgs)
If Not HasCheckedChildNodes(e.Node) Then
MsgBox(e.Node.Text)
End If
End Sub 'CheckForCheckedChildren
' Returns a value indicating whether the specified
' TreeNode has checked child nodes.
Private Function HasCheckedChildNodes(ByVal node As TreeNode) As Boolean
If node.Nodes.Count = 0 Then
Return False
End If
Dim childNode As TreeNode
For Each childNode In node.Nodes
If childNode.Checked Then
Return True
End If
' Recursively check the children of the current child node.
If HasCheckedChildNodes(childNode) Then
Return True
End If
Next childNode
Return False
End Function 'HasCheckedChildNodes
End Class
thx a lot..
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|