Force treeview parent node selection

Prysson

Registered User.
Local time
Today, 11:07
Joined
Sep 23, 2002
Messages
45
The following is my code on a on open evnt of a form containing a treeview.

IN the properties of the treeview I have selected to use check marks.


Currently you can select any node in the treeview and it will check individually with no effect on the parent node.

I need to find a way to force the parent node items to be checked with a child item is checked..

For example….

If the treeview is a function of location and it had three nodes

Area – Row – plot

And a user checked plot 1 in row 2 of area 1

Currently only plot one is checked..I need to make sure that if plot 1 is checked then by default area 1 and row 2 are checked. I do not however want to reverse that so that if if an perent node item is checked then all child nodes are selected.


Here is the code for the event that build the treeview



Private Sub Form_Load()
Dim rs As ADODB.Recordset
Set rs = New ADODB.Recordset
Dim nodX As Node

With rs
.ActiveConnection = CurrentProject.Connection
.CursorLocation = adUseServer
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT AreaID,AreaName FROM tblArea;"
End With

Do While Not rs.EOF
With Me!TreeViewLocation
.Nodes.Add , , "Area " & CStr(rs!AreaID) & " Node", rs!AreaName, 1
End With
rs.MoveNext
Loop

rs.Close
Set rs = Nothing

Set rs = New ADODB.Recordset
With rs
.ActiveConnection = CurrentProject.Connection
.CursorLocation = adUseServer
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT RowID,AreaID,RowNumber FROM tblRow;"
End With

Do While Not rs.EOF
With Me!TreeViewLocation
.Nodes.Add "Area " & CStr(rs!AreaID) & " Node", tvwChild, "Row " & CStr(rs!RowID) & " Node", rs!RowNumber, 2
End With
rs.MoveNext
Loop

rs.Close
Set rs = Nothing

Set rs = New ADODB.Recordset
With rs
.ActiveConnection = CurrentProject.Connection
.CursorLocation = adUseServer
.CursorType = adOpenKeyset
.LockType = adLockOptimistic
.Open "SELECT PlotID,RowID,Plot FROM tblPlot;"
End With

Do While Not rs.EOF
With Me!TreeViewLocation
.Nodes.Add "Row " & CStr(rs!RowID) & " Node", tvwChild, "Plot " & CStr(rs!PlotID) & " Node", rs!Plot, 3
End With
rs.MoveNext
Loop

rs.Close
Set rs = Nothing
End Sub
 
Ok I actually figured out how to force parent nodes to check but I cant figure out how to force child nodes to uncheck if the parent node is unchecked.

Could a modification of this code resolve the problem...may an else statement

Private Sub TreeView0_NodeCheck(ByVal Node As Object)
On Error Resume Next ' In case there are no parents or parent.parents
If Node.Checked Then
Node.Parent.Checked = True
Node.Parent.Parent.Checked = True
End If
End Sub

I dont know any ideas??
 
Ok I figured it out


For those interested I used this code

Private Sub TreeViewLocation_NodeCheck(ByVal Node As Object)
On Error Resume Next ' In case there are no parents or parent.parents
Dim NodX As Node, NodY As Node, NodZ As Node, strNodeKey As String

If Node.Checked Then
Node.Parent.Checked = True
Node.Parent.Parent.Checked = True
Else
strNodeKey = Node.Key
For Each NodX In Me!TreeViewLocation.Nodes
Set NodY = NodX.Parent
Set NodZ = NodY.Parent
If Not NodY Is Nothing Then
If NodY.Key = strNodeKey Then NodX.Checked = False
End If
If Not NodZ Is Nothing Then
If NodZ.Key = strNodeKey Then NodX.Checked = False
End If
Next NodX
End If
End Sub
 

Users who are viewing this thread

Back
Top Bottom