|
-
Sep 2nd, 2001, 09:21 PM
#1
Need help with Tree View Control
I need some help with the Tree View control. I set the properties so that the nodes have check boxes next to them.
When the parent node is checked, I want all the child nodes of that parent to be checked. Same with unchecking them. I won't know for sure how many child nodes there are. How do I do this in code, and do I do it in the NodeCheck event?
Some sample code would be a great help. Thanks.
-
Sep 2nd, 2001, 11:09 PM
#2
Frenzied Member
Try this code...
VB Code:
Public Sub CheckNodes(MyTreeView As TreeView, ByVal Node As MSComctlLib.Node, bChecked As Boolean)
Dim iIndex As Integer
If Not Node Is Nothing Then
If Node.Children > 0 Then
iIndex = Node.Child.Index
Node.Child.Checked = bChecked
CheckNodes MyTreeView, Node.Child, bChecked
While iIndex <> Node.Child.LastSibling.Index
MyTreeView.Nodes(iIndex).Next.Checked = bChecked
iIndex = MyTreeView.Nodes(iIndex).Next.Index
CheckNodes MyTreeView, MyTreeView.Nodes(iIndex).Next, bChecked
Wend
CheckNodes MyTreeView, Node.Child.Next, bChecked
Else
Node.Checked = bChecked
End If
End If
End Sub
Call from the TreeView_NodeCheck event like this
VB Code:
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
CheckNodes TreeView1, Node, Node.Checked
End Sub
Greg
Free VB Add-In - The Reference Librarian
Click Here for screen shot and download link.
-
Sep 3rd, 2001, 04:23 PM
#3
Thanks a lot for your help, it works great.
-
Sep 3rd, 2001, 09:45 PM
#4
hellswraith, be advised that MS acknowledges a bug in the TV control when checkboxes are used. They advise setting the Checkbox style of the TreeView by using API calls, rather than simply setting the "Checkboxes" property to True.
In a module, you would need:
Code:
Public Const TVS_CHECKBOXES As Long = &H100
Public Const GWL_STYLE As Long = (-16)
Public Declare Function GetWindowLong _
Lib "user32" Alias "GetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong _
Lib "user32" Alias "SetWindowLongA" _
(ByVal hwnd As Long, _
ByVal nIndex As Long, _
ByVal dwNewLong As Long) As Long
In your form (probably in the Load event), you would need:
Code:
Dim lngCurStyle As Long
Dim lngResult As Long
' === Set the Checkbox style of the TreeView ===
' As advised by Microsoft, due to a bug in the TreeView control,
' set the Checkbox style of the TreeView by using the following
' API calls, rather than simply setting the "Checkboxes" property
' to True ...
lngCurStyle = GetWindowLong(tvwGlobalUpdate.hwnd, GWL_STYLE)
lngResult = SetWindowLong(tvwGlobalUpdate.hwnd, GWL_STYLE, _
lngCurStyle Or TVS_CHECKBOXES)
Here's a link to the MS article:
http://support.microsoft.com/support.../Q192/1/88.asp
"It's cold gin time again ..."
Check out my website here.
-
Sep 3rd, 2001, 10:42 PM
#5
Thanks, I would probably would have been beating my head trying to figure out what is wrong if something happened. Good lookin out for me. That is why this forum is as good as it is.
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
|