Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Treeview Checked property won't check 2

Status
Not open for further replies.

gregoriw

Programmer
Sep 21, 2001
40
US
Hello all,

I have a situation where I'm trying to set the Checked property of node(s) in a treeview control based on a list of items from an array. This array holds prior selected/checked items from the treeview in a previous process.

When the user returns to view the treeview in another process run (maybe three days later), I want to set/check those items in the treeview (that were previously picked by the user) during the form_load event before the treeview and program are available to the user.

During the form_load event, I call the following routine with expectation that these previously selected items will show up in a Checked state.
===========================================
Private Sub CheckThisOut()
'pre-Check cokeys found in EEManualQACokeyList table
Dim i As Integer
Dim strTemp As String
Dim intLoop As Integer
Dim lngFound As Long

lngFound = 1
60 If UBound(mstrSelectedCokeys()) > 0 Then
For intLoop = 0 To UBound(mstrSelectedCokeys())
strTemp = mstrSelectedCokeys(intLoop)
'loop through each node finding the selected cokeys chosen
With TreeView1
For i = 1 To .Nodes.Count - 1
If .Nodes(i).Text Like "*" & strTemp & "*" Then
'set the checkbox in this node
.Nodes(i).Checked = True
' .Nodes(i).BackColor = vbButtonFace
.Nodes(i).EnsureVisible
.Nodes(i).Selected = True

DoEvents
'hang on to the first location checked
If lngFound = 1 Then lngFound = .Nodes(i).Index
Exit For
End If
Next i
End With

Next intLoop
TreeView1.Refresh
DoEvents
End If
mlngFirstSelCokey = lngFound
End Sub
==============================================
I tried setting another property (BackColor) as a test to make sure that the node is actually being edited and that works. But when I simply try to set .Checked = True, the checkbox on that node does not show up with a checkmark on it.

Is there another event that kicks off from the Checked property that prevents the checkmark from showing? Am I missing other calls that need to be made for the Checked property so that it will stick?

Thanks for any help,
gregoriw
 
It might be worth exploring the NodeCheck event to see if that event is fired.
 
Also, has the CheckBoxes property of the treeview has been set to True?
 
And, do you have any code in the nodecheck event itself? I do know that changing the checked status of a node inside of the nodecheck event is ignored once the event is exited, but otherwise it should work.

Robert
 
Here are the other responses to your questions:

1) TreeView1.CheckBoxes = True is set just after TreeView1.Nodes.Clear and before my looping routine fills in the tree list with values.

2) Put a breakpoint in NodeClick event and the routine above where a match is found and it never fired off when I set this assignment: TreeView.Nodes(i).Checked = True in the CheckThisOut routine that I listed in my original memo.

3) This is my NodeCheck code. I customized this routine so that if teh parent node was checked,
it would automatically check/uncheck any child nodes.

============================================================
Private Sub TreeView1_NodeCheck(ByVal Node As MSComctlLib.Node)
Const strPROCNAME As String = "TreeView1_NodeCheck"

Dim lngChildCount As Long
Dim lngIndex As Long
Dim blnNodeChecked As Boolean
Dim lngLoop As Long
Dim lngStart As Long

On Error GoTo ErrHandler

10 lngChildCount = Node.Children

'this event recognizes when a checkbox is activated, so try to force check/uncheck of any child nodes
12 If lngChildCount > 0 Then
' Debug.Print Node, Node.Checked, Node.Index, Node.Children
14 lngIndex = Node.Index
16 blnNodeChecked = Node.Checked

lngStart = lngIndex + 1
For lngLoop = lngStart To lngChildCount + lngIndex
20 TreeView1.Nodes(lngLoop).Checked = blnNodeChecked
Next

22 TreeView1.Refresh
End If
Exit Sub

ErrHandler:
Call CriticalErrorHandler(Err.Number, Err.Description, strPROCNAME, mstrModuleName, VBA.Erl)
End Sub
============================================================

I'm missing something real simple here...just can't find it. Thanks for helping.
gregoriw
 
> 2) Put a breakpoint in NodeClick event

Try a breakpoint in the nodeCHECK event, to see if it's firing and setting things correctly.

Robert
 
I did put a breakpoint in the NodeCheck event...I accidentally put down the NodeClick event in my item 2 above.

But even so, the NodeCheck event did not fire off when my checkmark assignment routine was running. Each time I F8 through the CheckThisOut routine and got to the point where the Nodes(i).Checked = True would execute, there never was a point where my debugging then put my cursor in the NodeCheck event. Just like it skipped over it. Not sure if it would even supposed to be fired off when assigning a value to the Checked property.

The only time I've gotten the NodeCheck event to fire is when I actually clicked on the node itself...and that was after the CheckThisOut routine had completed.

gregoriw
 
Well, a test shows that the checked event does not fire when the checking is done in code, which is different than regular checkboxes and some other controls. It's not like we expect things to act consistantly anyways....

But, as that may be, From a simple experiment using form_load and a sub like you have, I can get a treeview to check it's boxes and it works just fine.

I'm assuming that your form_load fills the treeview just before it calls your sub. Have you tried refreshing the tree just before the call is made?

Hope this helps...

Robert
 
This is the code I use to check all child nodes...

Code:
Private Sub List1_NodeCheck(ByVal Node As MSComctlLib.Node)
  Dim NODE2 As MSComctlLib.Node
  Set NODE2 = Node.Child
  If Not (NODE2 Is Nothing) Then
    Do
      NODE2.Checked = Node.Checked
      Set NODE2 = NODE2.Next
    Loop Until NODE2 Is Nothing
  End If
End Sub

you might need to set the checkbox option in the IDE...
Right click on your control in the form editor,
Select Properties,
Check the check box that says 'CheckBoxes' (5th one down on the right)
Click 'OK'

then double click on the control to go to the code for your treeview,
Select 'NodeCheck' from the drop down on the top right,
And insert the above code.

Hope this helps...


Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Still have SOME hair left....

Vampire, could you send me your test program in a text document so I can run it here and get some ideas of how you have this designed to make it work for you? Then maybe I can review what my code has in it and possibly adapt yours to make mine work. Worth a try at this point....I've wasted enough time on this silly little issue.

I also put TreeView1.Refresh in almost every routine and still no luck. Do I need to do something with the image property of the checkbox item in the node to refresh the image of the checkmark or something like that?

Thanks mucho,
gregoriw

"I still miss my ex-wife.....but my aim is improving."
 
*Note: I renamed the TreeView List1 in my program because It started out as a list and I later added a tree structure under the nodes...

to clear things up, here is a better worded example:

Code:
Private Sub Tree1_NodeCheck(ByVal Node As MSComctlLib.Node)
  Dim Child As MSComctlLib.Node
  Set Child = Node.Child
  If Not (Child Is Nothing) Then
    Do
      Child.Checked = Node.Checked
      Set Child = Child.Next
    Loop Until Child Is Nothing
  End If
End Sub

Good Luck,
-Josh

Have Fun, Be Young... Code BASIC
-Josh
cubee101.gif


PROGRAMMER: (n) Red-eyed, mumbling mammal capable of conversing with inanimate objects.
 
Gregoriw, have you had any luck with this issue? I am experiencing the same problem. The only twist is that I can select different user profiles on my form to repopulate the treeview. My code works fine when I call the populateTree() procedure after the form is opened but if I call the populateTree() procedure on the form load event my check boxes do not get checked.

Dan
 
During the Form_Load event you're not guaranteed to have all the controls available. At the end of the Form_load code add Me.Show and then call your populate code

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'
 
John, excellent post. That fixed up my problem right away.

The only thing I had to add aside from the "Me.Show" was a "DoEvents" call to wait for the controls to be loaded.

Thanks

Dan

Code:
If (blnValidateProfile(defMnu)) Then
    ' SET SELECTED MENUS CHECKED STATUS TO TRUE
    defMnu.Checked = True

    ' SHOW THE FORM
    Me.Show
    DoEvents

    ' CALL PROCEDURE TO POPULATE THE TREE
    Call populateTree
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top